zoukankan      html  css  js  c++  java
  • fastJson顺序遍历JSON字段(转)

    fastJson在把json格式的字符串转换成JSONObject的时候,使用的是HashMap,所以排序规则是根据HASH值排序的,如果想要按照字符串顺序遍历JSON属性,需要在转换的时候指定使用LinkedHashMap代替HashMap。

    以下为实例:

    public static void main(String[] args) {
            String jsonStr = "{"size":"7.5","width":"M (B)"}";
    
            System.out.println("无序遍历结果:");
            JSONObject jsonObj = JSON.parseObject(jsonStr);
            for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
    
            System.out.println("-------------------");
            System.out.println("有序遍历结果:");
            LinkedHashMap<String, String> jsonMap = JSON.parseObject(jsonStr, new TypeReference<LinkedHashMap<String, String>>() {
            });
            for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        }
  • 相关阅读:
    4.Docker Compose 部署 Nexus
    3.Docker Compose 部署 GitLab
    2.Docker Compose 部署应用程序
    1.Docker Compose
    6.Dockerfile 指令
    5.Dockerfile 定制镜像
    4.Docker 操作容器
    3.Docker 操作镜像
    2.Ubuntu安装 Docker
    windows快捷键
  • 原文地址:https://www.cnblogs.com/seely/p/5715512.html
Copyright © 2011-2022 走看看