Python 官方文档:入门教程 => 点击学习
JSONObject按put顺序排放与输出 jsONObject put数据之后,排序会发生变化 例如 JSONObject object=new JSONObject();
JSONObject object=new JSONObject();
object.put("aaa",111);
object.put("bbb",222);
object.put("ccc",333);
object.put("DDD",444);
输出结果可能为
{"aaa":111,"ddd":444,"ccc":333,"bbb":222}
因为JsonObject内部是用HashMap来存储的,所以输出是按key的排序来的,如果要让JsonObject按固定顺序(put的顺序)排列,可以修改JsonObject的定义HashMap改为LinkedHashMap。
public JSONObject() {
this.map = new LinkedHashMap(); //new HashMap();
}
即定义JsonObject可以这样:JSONObject jsonObj =new JSONObject(new LinkedHashMap());
JSONObject object=new JSONObject(new LinkedHashMap());
object.put("aaa",111);
object.put("bbb",222);
object.put("ccc",333);
object.put("ddd",444);
再次输出就会按顺序排了。
不知道大家想要的结果得到了没,反正我想要的结果已经得到。不解释,看下图--------->
net.sf.json.JSONObject
String timelineContent=一个json
Json.put("timelineContent",timelineContent);
此时框架会自动将String类型的timelineContent当Json put进去。。。
然后因为接口要求String不要JsonObject,就报
{"errorCode":-1,"errORMessage":"Internal Server Error","exceptionClassName":"org.springframework.Http.converter.HttpMessageNotReadableException","exceptionMessage":"JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 397] (through reference chain: java.util.ArrayList[0]->com.homethy.persistence.domain.LeadTimeline[\"timelineContent\"])","exceptionTime":"2019-06-19 08:59:12"}
没找指定类型为String不做转换的方法
Json.put("timelineContent",timelineContent==null?null:" "+timelineContent);
然后接口方就能正确识别了。。。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: JSONObject按put顺序排放与输出方式
本文链接: https://www.lsjlt.com/news/132249.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0