JSONArray和JSONObject的区别 是否有"[]"
,有的话是JSONArray,没有的话是JSONObject
1. 将 Array 解析成 Json 串。使用 JSONArray 可以解析 Array 类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 String[] str = { "Jack" , "Tom" , "90" , "true" }; JSONArray json = JSONArray.fromObject(str); System.err.println(json); Object[] o = { "北京" , "上海" , 89 , true , 90.87 }; json = JSONArray.fromObject(o); System.err.println(json); List<String> list = new ArrayList <String>(); list.add("Jack" ); list.add("Rose" ); json = JSONArray.fromObject(list); System.err.println(json); Set<Object> set = new HashSet <Object>(); set.add("Hello" ); set.add(true ); set.add(99 ); json = JSONArray.fromObject(set); System.err.println(json);
运行结果如下:
1 2 3 4 ["Jack","Tom","90","true"] ["北京","上海",89,true,90.87] ["Jack","Rose"] [99,true,"Hello"]
2. 将 JavaBean/Map 解析成 JSON 串。 使用JSONObject 解析: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Map<String, Object> map = new HashMap <String, Object>(); map.put("name" , "Tom" ); map.put("age" , 33 ); JSONObject jsonObject = JSONObject.fromObject(map); System.out.println(jsonObject); Person person = new Person ("A001" , "Jack" ); jsonObject = jsonObject.fromObject(person); System.out.println(jsonObject); map.put("person" , person); jsonObject = jsonObject.fromObject(map); System.out.println(jsonObject);
运行结果如下:
1 2 3 {"age":33,"name":"Tom"} {"id":"A001","name":"Jack"} {"person":{"id":"A001","name":"Jack"},"age":33,"name":"Tom"}
3. 使用 JsonConfig 过虑属性:适用于 JavaBean/Map 1 2 3 4 5 JsonConfig config = new JsonConfig (); config.setExcludes(new String [] { "name" }); Person person = new Person ("A001" , "Jack" ); JSONObject jsonObject = JSONObject.fromObject(person, config); System.out.println(jsonObject);
运行结果如下,在运行结果中我们可以看到 name 属性被过滤掉了:
4. 将 Json 串转换成 Array: 1 2 3 4 JSONArray jsonArray = JSONArray.fromObject("[89,90,99]" ); Object array = JSONArray.toArray(jsonArray); System.out.println(array); System.out.println(Arrays.asList((Object[]) array));
运行结果如下:
1 2 [Ljava.lang.Object;@1e5003f6 [89, 90, 99]
5. 将 Json 串转成 JavaBean/Map: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 String str = "{\"name\":\"Tom\",\"age\":90}" ; JSONObject jsonObject = JSONObject.fromObject(str); Map<String, Object> map = (Map<String, Object>) JSONObject.toBean(jsonObject, Map.class); System.out.println(map); str = "{\"id\":\"A001\",\"name\":\"Jack\"}" ; jsonObject = JSONObject.fromObject(str); System.out.println(jsonObject); Person person = (Person) JSONObject.toBean(jsonObject, Person.class); System.out.println(person);
运行结果如下:
1 2 {age=90, name=Tom} Person [id=A001, name=Jack]
在将 Json 形式的字符串转换为 JavaBean 的时候需要注意 JavaBean 中必须有无参构造函数,否则会报如下找不到初始化方法的错误: 1 2 3 4 5 6 7 8 9 10 Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: cn.sunzn.json.Person.<init>() at net.sf.json.JSONObject.toBean(JSONObject.java:288 ) at net.sf.json.JSONObject.toBean(JSONObject.java:233 ) at cn.sunzn.json.JsonLib.main(JsonLib.java:23 ) Caused by: java.lang.NoSuchMethodException: cn.sunzn.json.Person.<init>() at java.lang.Class.getConstructor0(Unknown Source) at java.lang.Class.getDeclaredConstructor(Unknown Source) at net.sf.json.util.NewBeanInstanceStrategy$DefaultNewBeanInstanceStrategy.newInstance(NewBeanInstanceStrategy.java:55 ) at net.sf.json.JSONObject.toBean(JSONObject.java:282 ) ... 2 more