使用JSONObject  读取JSON字符串时,读取出来的数据,通过变量时是无序的。

但是业务希望返回是顺序的。

String str="{name:\"A\",address:\"B\",tel:\"C\",user:{name:\"A\",address:\"B\"}}";
JSONObject json=JSONObject.parseObject(str);

解决办法如下:

LinkedHashMap<String, Object> map= JSON.parseObject(str,new TypeReference<LinkedHashMap<String, Object>>(){}, Feature.OrderedField);

使用如上代码,这样map 返回都数据就是按照 json 的原来顺序进行输出。

相关文章:

  • 2021-11-17
  • 2021-07-06
  • 2021-11-16
  • 2021-08-22
  • 2021-10-18
  • 2022-02-19
  • 2022-01-17
猜你喜欢
  • 2022-12-23
  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
  • 2021-10-05
  • 2021-09-30
相关资源
相似解决方案