fastJson JSON.parseObject()丢失字符串原本顺序

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());
        }

 LinkedHashMap<String, String> jsonMap2 = JSON.parseObject(jsonStr, new TypeReference<LinkedHashMap<String, String>>() {
        },Feature.OrderedField);
        for (Map.Entry<String, String> entry : jsonMap2.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }

解决方式:
 

LinkedHashMap<String, Object> contentMap = JSON.parseObject(reportContent, LinkedHashMap.class, Feature.OrderedField);

 

posted @ 2019-09-04 21:22  柚子=_=  阅读(1431)  评论(0编辑  收藏  举报