Java不像PHP解析和生产JSON总是一个比较痛苦的过程。但是使用JSONObject和JSONArray会让整个过程相对舒服一些。
需要依赖的包:commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
1. 创建一个JSONObject对象:
package com.xxx.video.resource.controller.web;
02.
03.import java.util.ArrayList;
04.import java.util.HashMap;
05.
06.import net.sf.json.JSONArray;
07.import net.sf.json.JSONObject;
08.
09.
10.public class Test {
11.
12. public static void main(String[] args) {
13. //JsonObject和JsonArray区别就是JsonObject是对象形式,JsonArray是数组形式
14. //创建JsonObject第一种方法
15. JSONObject jsonObject = new JSONObject();
16. jsonObject.put("UserName", "ZHULI");
17. jsonObject.put("age", "30");
18. jsonObject.put("workIn", "ALI");
19. System.out.println("jsonObject1:" + jsonObject);
20.
21. //创建JsonObject第二种方法
22. HashMap<String, String> hashMap = new HashMap<String, String>();
23. hashMap.put("UserName", "ZHULI");
24. hashMap.put("age", "30");
25. hashMap.put("workIn", "ALI");
26. System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));
27.
28. //创建一个JsonArray方法1
29. JSONArray jsonArray = new JSONArray();
30. jsonArray.add(0, "ZHULI");
31. jsonArray.add(1, "30");
32. jsonArray.add(2, "ALI");
33. System.out.println("jsonArray1:" + jsonArray);
34.
35. //创建JsonArray方法2
36. ArrayList<String> arrayList = new ArrayList<String>();
37. arrayList.add("ZHULI");
38. arrayList.add("30");
39. arrayList.add("ALI");
40. System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));
41.
42. //如果JSONArray解析一个HashMap,则会将整个对象的放进一个数组的值中
43. System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));
44.
45. //组装一个复杂的JSONArray
46. JSONObject jsonObject2 = new JSONObject();
47. jsonObject2.put("UserName", "ZHULI");
48. jsonObject2.put("age", "30");
49. jsonObject2.put("workIn", "ALI");
50. jsonObject2.element("Array", arrayList);
51. System.out.println("jsonObject2:" + jsonObject2);
52.
53. }
54.}
结果:
jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
02.jsonObject2:{"workIn":"ALI","age":"30","UserName":"ZHULI"}
03.jsonArray1:["ZHULI","30","ALI"]
04.jsonArray2:["ZHULI","30","ALI"]
05.jsonArray FROM HASHMAP:[{"workIn":"ALI","age":"30","UserName":"ZHULI"}]
06.jsonObject2:{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"]}
解析JSON字符串:
package com.xxx.video.resource.controller.web;
02.
03.import net.sf.json.JSONArray;
04.import net.sf.json.JSONObject;
05.
06.
07.public class Test {
08.
09. public static void main(String[] args) {
10. String jsonString = "{\"UserName\":\"ZHULI\",\"age\":\"30\",\"workIn\":\"ALI\",\"Array\":[\"ZHULI\",\"30\",\"ALI\"]}";
11. //将Json字符串转为java对象
12. JSONObject obj = JSONObject.fromObject(jsonString);
13. //获取Object中的UserName
14. if (obj.has("UserName")) {
15. System.out.println("UserName:" + obj.getString("UserName"));
16. }
17. //获取ArrayObject
18. if (obj.has("Array")) {
19. JSONArray transitListArray = obj.getJSONArray("Array");
20. for (int i = 0; i < transitListArray.size(); i++) {
21. System.out.print("Array:" + transitListArray.getString(i) + " ");
22. }
23. }
24. }
25.}
返回
UserName:ZHULI
02.Array:ZHULI Array:30 Array:ALI
对象遍历
if (!obj.isNull("sku")) {
02. HashMap<String, String> skuMap = new HashMap<String, String>();
03. JSONObject skuObject = obj.getJSONObject("sku");
04. Iterator<String> keys = skuObject.keys();
05. String key;
06. Object o;
07. while (keys.hasNext()) {
08. key = keys.next();
09. o = skuObject.get(key);
10. skuMap.put(key, String.valueOf(o));
11. }
12.}