使用JSONObject和JSONArray

前端之家收集整理的这篇文章主要介绍了使用JSONObject和JSONArray前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JSONObject

JSONObject是一个无序的键值对集合。值可以是任意类型:Boolean,JSONArray,JSONObject,Number,String,or the JSONObject.NULL Object.

http://maps.google.com/maps/api/geocode/json?sensor=false&address=beijing 返回的json如下:

  1. {
  2. "results" : [ { "address_components" : [ { "long_name" : "北京","short_name" : "北京","types" : [ "locality","political" ] },{ "long_name" : "北京市","short_name" : "北京市","types" : [ "administrative_area_level_1",{ "long_name" : "中国","short_name" : "CN","types" : [ "country","political" ] } ],"formatted_address" : "中国北京市北京市","geometry" : { "bounds" : { "northeast" : { "lat" : 41.0608158,"lng" : 117.5146251 },"southwest" : { "lat" : 39.4427581,"lng" : 115.4234115 } },"location" : { "lat" : 39.904211,"lng" : 116.407395 },"location_type" : "APPROXIMATE","viewport" : { "northeast" : { "lat" : 40.2164962,"lng" : 116.7829835 },"southwest" : { "lat" : 39.6612714,"lng" : 116.0119343 } } },"place_id" : "ChIJuSwU55ZS8DURiqkPryBWYrk","status" : "OK" }

JSONArray

JSONArray是一个有序的值序列。值可以是任意类型:Boolean,or the JSONNull object。

形如:

  1. [ "locality","political" ]

实例:

  1. import java.util.ArrayList;
  2. import java.util.LinkedHashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import net.sf.json.JSONArray;
  7. import net.sf.json.JSONObject;
  8.  
  9. public class JsonObjectArrayTest {
  10.  
  11. public static void main(String[] args) {
  12. // 1.从字符串转换成JSONArray对象,必须首尾为[,]; 工具代码会判断是否含有'['和']'
  13. String types1 = "[\"locality\",\"political\"]";
  14. JSONArray jsonArray1 = JSONArray.fromObject(types1);
  15. System.out.println(jsonArray1.toString());
  16.  
  17. // 2.从一个JSONArray转换成另一个JSONArray对象
  18. JSONArray jsonArray2 = JSONArray.fromObject(jsonArray1);
  19. System.out.println(jsonArray2.toString());
  20.  
  21. // 3.将Collection对象,本例为ArrayList转换成JSONArray对象
  22. List<String> types3 = new ArrayList<String>();
  23. types3.add("locality");
  24. types3.add("political");
  25. JSONArray jsonArray3 = JSONArray.fromObject(types3);
  26. System.out.println(jsonArray3.toString());
  27.  
  28. // 4. new 一个JSONObject对象,使用put方法添加成员,如果已经存在key,则替换value值
  29. JSONObject jsonObject1 = new JSONObject();
  30. jsonObject1.put("types",jsonArray3);
  31. System.out.println(jsonObject1.toString());
  32.  
  33. // 5.将一个JSONObject对象转换成另一个JSONObject对象
  34. JSONObject jsonObject2 = JSONObject.fromObject(jsonObject1);
  35. System.out.println(jsonObject2);
  36.  
  37. // 6.将一Map对象转换成JSONObject对象
  38. Map<String,List<String>> map = new LinkedHashMap<String,List<String>>();
  39. List<String> list = new ArrayList<String>();
  40. list.add("locality");
  41. list.add("political");
  42. map.put("types",list);
  43. JSONObject jsonObject3 = JSONObject.fromObject(map);
  44. System.out.println(jsonObject3);
  45.  
  46. // 7.将String转换成JSONObject对象
  47. String str1 = "{\"types\":[\"locality\",\"political\"]}";
  48. JSONObject jsonObject4 = JSONObject.fromObject(str1);
  49. System.out.println(jsonObject4);
  50.  
  51. // 8.累积value到这个key下,这个key下value变成JSONArray, 如果value本身就是JSONArray,直接添加到里面
  52. JSONObject jsonObject5 = jsonObject4.accumulate("place_id","ChIJuSwU55ZS8DURiqkPryBWYrk");
  53. System.out.println(jsonObject5.toString());
  54. JSONObject jsonObject6 = jsonObject5.accumulate("place_id","accumulate");
  55. System.out.println(jsonObject5.toString());
  56. System.out.println(jsonObject6);
  57. // 9.element将键值对放到JSONObject中,如果key存在,则替换原来的value;
  58. // element与put的区别: 1.put返回的Object对象; element返回的是JSONObject对象
  59. // 2.put的key为Object对象,element的key为String
  60. JSONObject jsonObject7 = jsonObject6.element("types",1);
  61. System.out.println(jsonObject7.toString());
  62.  
  63. // 10.使用get方法获取成员
  64. System.out.println(jsonObject7.getInt("types"));
  65. System.out.println(jsonObject7.getJSONArray("place_id"));
  66. System.out.println(jsonObject7.getJSONArray("place_id").get(1));
  67.  
  68.  
  69. }
  70.  
  71. }

输出

  1. ["locality","political"]
  2. ["locality","political"]
  3. {"types":["locality","political"]}
  4. {"types":["locality","political"],"place_id":"ChIJuSwU55ZS8DURiqkPryBWYrk"}
  5. {"types":["locality","place_id":["ChIJuSwU55ZS8DURiqkPryBWYrk","accumulate"]}
  6. {"types":["locality","accumulate"]}
  7. {"types":1,"accumulate"]}
  8. 1
  9. ["ChIJuSwU55ZS8DURiqkPryBWYrk","accumulate"]
  10. accumulate

总结:

JSONObject添加成员的方法element()与put()的区别:

1.put返回的Object对象,element返回的是JSONObject对象

2.put的key为Object对象,element的key为String

本文介绍的方法是net.sf.json下的JSONObject和JSONArray, 而非org.json下。

wiki:

http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONObject.html
http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONArray.html

猜你在找的Json相关文章