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 entry : jsonObj.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        System.out.println("-------------------");
        System.out.println("有序遍历结果:");
        LinkedHashMap jsonMap = JSON.parseObject(jsonStr, new TypeReference>() {
        });
        for (Map.Entry entry : jsonMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

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

解决方式:
 

LinkedHashMap contentMap = JSON.parseObject(reportContent, LinkedHashMap.class, Feature.OrderedField);

 

你可能感兴趣的:(java)