Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
输出为:
{"arr":["a","b"],"int":1,"bool":true,"name":"json"}暂时可以理解成,取的时候是从后往前取在看例子:Map map = new HashMap();
map.put("name", "json");
map.put("arr", new String[] { "a", "b" });
map.put("int", new Integer(1));
map.put("bool", Boolean.TRUE);
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);输出为:
{"arr":["a","b"],"int":1,"bool":true,"name":"json"}可以看出:数组默认在前,int其次,然后就是boolean和String现在想请问下如何按照我想输出的类型输出
比如说,先是String,然后是数组和boolean
不知道什么场景需要直接输出json串?
并且格式是我可以控制的
重载toString?有意义么?Map是串行化以后被当做字节流发送到浏览器的,跟toString会有关系么?
自己有写,但是先改成json格式的
可以使用LinkedHashMap,这个实现为维持顺序的