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