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