JSON

json解析的时候,通常顺序会发生改变,如果我们有保持原有顺序的需求的话可以通过如下方式

1、使用fastJson的Feature.OrderedField

JSONObject respJson = JSONObject.parseObject(jsonStr, Feature.OrderedField);

2、初始化JSONObject的时候【new JSONObject(true)】(实测可用)

1.JSONObject jsonObject = new JSONObject(true);
2.fastjson版本大于1.2.3
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>

JSONObject jsonObject = new JSONObject(true);

3、可以使用Google的Gson,使用gson解析的话是怎么传就是什么顺序

JsonObject jsonObject = new JsonParser().parse(jsonStr).getAsJsonObject();
原文地址:https://www.cnblogs.com/gzhbk/p/13999912.html