正常转换:
public static void main(String[] args) {
String jsonDoc = "{\"b\":2,\"a\":1,\"2\":1,\"1\":2,\"c\":2}";
JSONObject jsonObj = JSONObject.parseObject(jsonDoc);
System.out.println(jsonObj);
}
输出结果,很明显和原始数据顺序不一样:
{"a":1,"1":2,"b":2,"2":1,"c":2}
优化后:
public static void main(String[] args) {
String jsonDoc = "{\"b\":2,\"a\":1,\"2\":1,\"1\":2,\"c\":2}";
JSONObject jsonObj = JSONObject.parseObject(jsonDoc, Feature.OrderedField);
System.out.println(jsonObj);
}
输出结果和原始字符串保持一致了:
{"b":2,"a":1,"2":1,"1":2,"c":2}