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);
正因为当初对未来做了太多的憧憬,所以对现在的自己尤其失望。生命中曾经有过的所有灿烂,终究都需要用寂寞来偿还。