|
1 Simple Java toolkit for JSON (JSON.simple) |
|
2 ========================================== |
|
3 |
|
4 1.Why the Simple Java toolkit (also named as JSON.simple) for JSON? |
|
5 |
|
6 When I use JSON as the data exchange format between the AJAX client and JSP |
|
7 for the first time, what worry me mostly is how to encode Java strings and |
|
8 numbers correctly in the server side so the AJAX client will receive a well |
|
9 formed JSON data. When I looked into the 'JSON in Java' directory in JSON |
|
10 website,I found that wrappers to JSONObject and JSONArray can be simpler, |
|
11 due to the simplicity of JSON itself. So I wrote the JSON.simple package. |
|
12 |
|
13 2.Is it simple,really? |
|
14 |
|
15 I think so. Take an example: |
|
16 |
|
17 import org.json.simple.JSONObject; |
|
18 |
|
19 JSONObject obj=new JSONObject(); |
|
20 obj.put("name","foo"); |
|
21 obj.put("num",new Integer(100)); |
|
22 obj.put("balance",new Double(1000.21)); |
|
23 obj.put("is_vip",new Boolean(true)); |
|
24 obj.put("nickname",null); |
|
25 System.out.print(obj); |
|
26 |
|
27 Result: |
|
28 {"nickname":null,"num":100,"balance":1000.21,"is_vip":true,"name":"foo"} |
|
29 |
|
30 The JSONObject.toString() will escape controls and specials correctly. |
|
31 |
|
32 3.How to use JSON.simple in JSP? |
|
33 |
|
34 Take an example in JSP: |
|
35 |
|
36 <%@page contentType="text/html; charset=UTF-8"%> |
|
37 <%@page import="org.json.simple.JSONObject"%> |
|
38 <% |
|
39 JSONObject obj=new JSONObject(); |
|
40 obj.put("name","foo"); |
|
41 obj.put("num",new Integer(100)); |
|
42 obj.put("balance",new Double(1000.21)); |
|
43 obj.put("is_vip",new Boolean(true)); |
|
44 obj.put("nickname",null); |
|
45 out.print(obj); |
|
46 out.flush(); |
|
47 %> |
|
48 |
|
49 So the AJAX client will get the responseText. |
|
50 |
|
51 4.Some details about JSONObject? |
|
52 |
|
53 JSONObject inherits java.util.HashMap,so it don't have to worry about the |
|
54 mapping things between keys and values. Feel free to use the Map methods |
|
55 like get(), put(), and remove() and others. JSONObject.toString() will |
|
56 combine key value pairs to get the JSON data string. Values will be escaped |
|
57 into JSON quote string format if it's an instance of java.lang.String. Other |
|
58 type of instance like java.lang.Number,java.lang.Boolean,null,JSONObject and |
|
59 JSONArray will NOT escape, just take their java.lang.String.valueOf() result. |
|
60 null value will be the JSON 'null' in the result. |
|
61 |
|
62 It's still correct if you put an instance of JSONObject or JSONArray into an |
|
63 instance of JSONObject or JSONArray. Take the example about: |
|
64 |
|
65 JSONObject obj2=new JSONObject(); |
|
66 obj2.put("phone","123456"); |
|
67 obj2.put("zip","7890"); |
|
68 obj.put("contact",obj2); |
|
69 System.out.print(obj); |
|
70 |
|
71 Result: |
|
72 {"nickname":null,"num":100,"contact":{"phone":"123456","zip":"7890"},"balance":1000.21,"is_vip":true,"name":"foo"} |
|
73 |
|
74 The method JSONObject.escape() is used to escape Java string into JSON quote |
|
75 string. Controls and specials will be escaped correctly into \b,\f,\r,\n,\t, |
|
76 \",\\,\/,\uhhhh. |
|
77 |
|
78 5.Some detail about JSONArray? |
|
79 |
|
80 org.json.simple.JSONArray inherits java.util.ArrayList. Feel free to use the |
|
81 List methods like get(),add(),remove(),iterator() and so on. The rules of |
|
82 JSONArray.toString() is similar to JSONObject.toString(). Here's the example: |
|
83 |
|
84 import org.json.simple.JSONArray; |
|
85 |
|
86 JSONArray array=new JSONArray(); |
|
87 array.add("hello"); |
|
88 array.add(new Integer(123)); |
|
89 array.add(new Boolean(false)); |
|
90 array.add(null); |
|
91 array.add(new Double(123.45)); |
|
92 array.add(obj2);//see above |
|
93 System.out.print(array); |
|
94 |
|
95 Result: |
|
96 ["hello",123,false,null,123.45,{"phone":"123456","zip":"7890"}] |
|
97 |
|
98 6.What is JSONValue for? |
|
99 |
|
100 org.json.simple.JSONValue is use to parse JSON data into Java Object. |
|
101 In JSON, the topmost entity is JSON value, not the JSON object. But |
|
102 it's not necessary to wrap JSON string,boolean,number and null again, |
|
103 for the Java has already had the according classes: java.lang.String, |
|
104 java.lang.Boolean,java.lang.Number and null. The mapping is: |
|
105 |
|
106 JSON Java |
|
107 ------------------------------------------------ |
|
108 string <=> java.lang.String |
|
109 number <=> java.lang.Number |
|
110 true|false <=> java.lang.Boolean |
|
111 null <=> null |
|
112 array <=> org.json.simple.JSONArray |
|
113 object <=> org.json.simple.JSONObject |
|
114 ------------------------------------------------ |
|
115 |
|
116 JSONValue has only one kind of method, JSONValue.parse(), which receives |
|
117 a java.io.Reader or java.lang.String. Return type of JSONValue.parse() |
|
118 is according to the mapping above. If the input is incorrect in syntax or |
|
119 there's exceptions during the parsing, I choose to return null, ignoring |
|
120 the exception: I have no idea if it's a serious implementaion, but I think |
|
121 it's convenient to the user. |
|
122 |
|
123 Here's the example: |
|
124 |
|
125 String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; |
|
126 Object obj=JSONValue.parse(s); |
|
127 JSONArray array=(JSONArray)obj; |
|
128 System.out.println(array.get(1)); |
|
129 JSONObject obj2=(JSONObject)array.get(1); |
|
130 System.out.println(obj2.get("1")); |
|
131 |
|
132 Result: |
|
133 {"1":{"2":{"3":{"4":[5,{"6":7}]}}}} |
|
134 {"2":{"3":{"4":[5,{"6":7}]}}} |
|
135 |
|
136 7.About the author. |
|
137 |
|
138 I'm a Java EE developer on Linux. |
|
139 I'm working on web systems and information retrieval systems. |
|
140 I also develop 3D games and Flash games. |
|
141 |
|
142 You can contact me through: |
|
143 Fang Yidong<fangyidong@yahoo.com.cn> |
|
144 Fang Yidong<fangyidng@gmail.com> |