JAVA对象与JSON之间的转换

1.List集合转换成json代码

List list = new ArrayList();

list.add(“first”);

list.add(“second”);

JSONArray jsonArray2 = JSONArray.fromObject(list);

2.Map集合转换成json代码

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”});

(“func”,“function(i){return this.arr [i];}”);

JSONObject json = JSONObject.fromObject(map);

3.Bean转换成json代码

JSONObject jsonObject = JSONObject.fromObject(new JsonBean());

4.数组转换为json代码

boolean [] boolArray = new boolean [] {true,false,true};

JSONArray jsonArray1 = JSONArray.fromObject(boolArray);

问题:在转换的过程中,分别使用了JSONArray.fromObject()和JSONObject.fromObject(),这两者有什么区别?

解答:https://mp.csdn.net/postedit/81623621

猜你喜欢

转载自blog.csdn.net/MyBloggerlxs/article/details/81623573