json-lib.jar开发包使用:
依赖包:
commons-beanutils.jar;
commons-httpclient.jar;
commons-lang.jar;
ezmorph.jar;不少人使用时会提示net.sf.ezmorph.xxx找不到,就是缺这个:
morph-1.0.1.jar
使用过程中问题:
1,把bean转化为json格式时老提示如下错误:
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'name' has no getter method
解决:声明bean为public class xxx,必须是public,我用默认类型(class xxx)都不行
2,Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.lang.ArrayUtils.toObject([C)[Ljava/lang/Character;
原因:定义属性如下:private char[] options = new char[] { 'a', 'f' };好像不能处理这种类型的
3, private String func1 = "function(i){ return this.options[i]; }";
和
private JSONFunction func2 = new JSONFunction(new String[] { "i" },
"return this.options[i];");
转换后显示结果差不多:
{"func1":function(i){ return this.options[i];,"func2":function(i){ return this.options[i]; }}
测试类:
1. import
2. import
3. import
4. import
5.
6. import
7. import
8.
9. public class
10. public static void
11. new
12. j.bean2json();
13. }
14.
15. public void
16. boolean[] boolArray = new boolean[] { true, false, true
17. JSONArray jsonArray = JSONArray.fromObject(boolArray);
18. System.out.println(jsonArray);
19. // prints [true,false,true]
20. }
21.
22. public void
23. new
24. "first");
25. "second");
26. JSONArray jsonArray = JSONArray.fromObject(list);
27. System.out.println(jsonArray);
28. // prints ["first","second"]
29. }
30.
31. public void
32. "['json','is','easy']");
33. System.out.println(jsonArray);
34. // prints ["json","is","easy"]
35. }
36.
37. public void
38. Map
39. "name", "json");
40. "bool", Boolean.TRUE);
41. "int", new Integer(1));
42. "arr", new String[] { "a", "b"
43. "func", "function(i){ return this.arr[i]; }");
44.
45. JSONObject json = JSONObject.fromObject(map);
46. System.out.println(json);
47. // prints
48. // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
49. // return this.arr[i]; }]
50. }
51.
52. public void
53. new
54. System.out.println(jsonObject);
55. /*
56. * prints
57. * {"func1":function(i){ return this.options[i];
58. * },"pojoId":1,"name":"json","func2":function(i){ return
59. * this.options[i]; }}
60. */
61. }
62.
63. public void
64. "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
65. JSONObject jb = JSONObject.fromString(json);
66. class);
67. System.out.println();
68. }
69. }
操作的bean:
1. import
2.
3. public class
4. private String name = "json";
5. private int pojoId = 1;
6. // private char[] options = new char[] { 'a', 'f' };
7. private String func1 = "function(i){ return this.options[i]; }";
8. private JSONFunction func2 = new JSONFunction(new String[] { "i"
9. "return this.options[i];");
10.
11. // getters & setters
12. ......
13. }
题外话: 这个我对json-lib包的初次尝试,希望对大家有所帮助,另外大家有谁用过其它处理json的开发包,提出来,大家探讨一下~!!!!
=============
java中使用net.sf.json对json进行解析
2010-08-02 15:44
1869人阅读
评论(0)
收藏
举报
net.sf.json依赖的包很多。
有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar
1. /**
2. * 从一个JSON 对象字符格式中得到一个java对象,形如:
3. * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
4. * @param object
5. * @param clazz
6. * @return
7. */
8. public static
9. null;
10. try{
11. setDataFormat2JAVA();
12. jsonObject = JSONObject.fromObject(jsonString);
13. catch(Exception e){
14. e.printStackTrace();
15. }
16. return
17. }
18.
19. /**
20. * 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:
21. * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},
22. * beansList:[{}, {}, ...]}
23. * @param jsonString
24. * @param clazz
25. * @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)
26. * @return
27. */
28. public static
29. null;
30. try{
31. setDataFormat2JAVA();
32. jsonObject = JSONObject.fromObject(jsonString);
33. catch(Exception e){
34. e.printStackTrace();
35. }
36. return
37. }
38.
39. /**
40. * 从一个JSON数组得到一个java对象数组,形如:
41. * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
42. * @param object
43. * @param clazz
44. * @return
45. */
46. public static
47. setDataFormat2JAVA();
48. JSONArray array = JSONArray.fromObject(jsonString);
49. new
50. for(int i = 0; i < array.size(); i++){
51. JSONObject jsonObject = array.getJSONObject(i);
52. obj[i] = JSONObject.toBean(jsonObject, clazz);
53. }
54. return
55. }
56.
57. /**
58. * 从一个JSON数组得到一个java对象数组,形如:
59. * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
60. * @param object
61. * @param clazz
62. * @param map
63. * @return
64. */
65. public static
66. setDataFormat2JAVA();
67. JSONArray array = JSONArray.fromObject(jsonString);
68. new
69. for(int i = 0; i < array.size(); i++){
70. JSONObject jsonObject = array.getJSONObject(i);
71. obj[i] = JSONObject.toBean(jsonObject, clazz, map);
72. }
73. return
74. }
75.
76. /**
77. * 从一个JSON数组得到一个java对象集合
78. * @param object
79. * @param clazz
80. * @return
81. */
82. public static
83. setDataFormat2JAVA();
84. JSONArray array = JSONArray.fromObject(jsonString);
85. new
86. for(Iterator iter = array.iterator(); iter.hasNext();){
87. JSONObject jsonObject = (JSONObject)iter.next();
88. list.add(JSONObject.toBean(jsonObject, clazz));
89. }
90. return
91. }
92.
93. /**
94. * 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性
95. * @param object
96. * @param clazz
97. * @param map 集合属性的类型
98. * @return
99. */
100. public static
101. setDataFormat2JAVA();
102. JSONArray array = JSONArray.fromObject(jsonString);
103. new
104. for(Iterator iter = array.iterator(); iter.hasNext();){
105. JSONObject jsonObject = (JSONObject)iter.next();
106. list.add(JSONObject.toBean(jsonObject, clazz, map));
107. }
108. return
109. }
110.
111. /**
112. * 从json HASH表达式中获取一个map,该map支持嵌套功能
113. * 形如:{"id" : "johncon", "name" : "小强"}
114. * 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap
115. * @param object
116. * @return
117. */
118. public static
119. setDataFormat2JAVA();
120. JSONObject jsonObject = JSONObject.fromObject(jsonString);
121. new
122. for(Iterator iter = jsonObject.keys(); iter.hasNext();){
123. String key = (String)iter.next();
124. map.put(key, jsonObject.get(key));
125. }
126. return
127. }
128.
129. /**
130. * 从json数组中得到相应java数组
131. * json形如:["123", "456"]
132. * @param jsonString
133. * @return
134. */
135. public static
136. JSONArray jsonArray = JSONArray.fromObject(jsonString);
137. return
138. }
139.
140.
141. /**
142. * 把数据对象转换成json字符串
143. * DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
144. * 数组对象形如:[{}, {}, {}, ...]
145. * map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...}
146. * @param object
147. * @return
148. */
149. public static String getJSONString(Object object) throws
150. null;
151. //日期值处理器
152. new
153. java.util.Date.class, new
154. if(object != null){
155. if(object instanceof Collection || object instanceof
156. jsonString = JSONArray.fromObject(object, jsonConfig).toString();
157. else{
158. jsonString = JSONObject.fromObject(object, jsonConfig).toString();
159. }
160. }
161. return jsonString == null ? "{}"
162. }
net.sf.json依赖的包很多。
有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar
1. /**
2. * 从一个JSON 对象字符格式中得到一个java对象,形如:
3. * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
4. * @param object
5. * @param clazz
6. * @return
7. */
8. public static
9. null;
10. try{
11. setDataFormat2JAVA();
12. jsonObject = JSONObject.fromObject(jsonString);
13. catch(Exception e){
14. e.printStackTrace();
15. }
16. return
17. }
18.
19. /**
20. * 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:
21. * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},
22. * beansList:[{}, {}, ...]}
23. * @param jsonString
24. * @param clazz
25. * @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)
26. * @return
27. */
28. public static
29. null;
30. try{
31. setDataFormat2JAVA();
32. jsonObject = JSONObject.fromObject(jsonString);
33. catch(Exception e){
34. e.printStackTrace();
35. }
36. return
37. }
38.
39. /**
40. * 从一个JSON数组得到一个java对象数组,形如:
41. * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
42. * @param object
43. * @param clazz
44. * @return
45. */
46. public static
47. setDataFormat2JAVA();
48. JSONArray array = JSONArray.fromObject(jsonString);
49. new
50. for(int i = 0; i < array.size(); i++){
51. JSONObject jsonObject = array.getJSONObject(i);
52. obj[i] = JSONObject.toBean(jsonObject, clazz);
53. }
54. return
55. }
56.
57. /**
58. * 从一个JSON数组得到一个java对象数组,形如:
59. * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
60. * @param object
61. * @param clazz
62. * @param map
63. * @return
64. */
65. public static
66. setDataFormat2JAVA();
67. JSONArray array = JSONArray.fromObject(jsonString);
68. new
69. for(int i = 0; i < array.size(); i++){
70. JSONObject jsonObject = array.getJSONObject(i);
71. obj[i] = JSONObject.toBean(jsonObject, clazz, map);
72. }
73. return
74. }
75.
76. /**
77. * 从一个JSON数组得到一个java对象集合
78. * @param object
79. * @param clazz
80. * @return
81. */
82. public static
83. setDataFormat2JAVA();
84. JSONArray array = JSONArray.fromObject(jsonString);
85. new
86. for(Iterator iter = array.iterator(); iter.hasNext();){
87. JSONObject jsonObject = (JSONObject)iter.next();
88. list.add(JSONObject.toBean(jsonObject, clazz));
89. }
90. return
91. }
92.
93. /**
94. * 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性
95. * @param object
96. * @param clazz
97. * @param map 集合属性的类型
98. * @return
99. */
100. public static
101. setDataFormat2JAVA();
102. JSONArray array = JSONArray.fromObject(jsonString);
103. new
104. for(Iterator iter = array.iterator(); iter.hasNext();){
105. JSONObject jsonObject = (JSONObject)iter.next();
106. list.add(JSONObject.toBean(jsonObject, clazz, map));
107. }
108. return
109. }
110.
111. /**
112. * 从json HASH表达式中获取一个map,该map支持嵌套功能
113. * 形如:{"id" : "johncon", "name" : "小强"}
114. * 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap
115. * @param object
116. * @return
117. */
118. public static
119. setDataFormat2JAVA();
120. JSONObject jsonObject = JSONObject.fromObject(jsonString);
121. new
122. for(Iterator iter = jsonObject.keys(); iter.hasNext();){
123. String key = (String)iter.next();
124. map.put(key, jsonObject.get(key));
125. }
126. return
127. }
128.
129. /**
130. * 从json数组中得到相应java数组
131. * json形如:["123", "456"]
132. * @param jsonString
133. * @return
134. */
135. public static
136. JSONArray jsonArray = JSONArray.fromObject(jsonString);
137. return
138. }
139.
140.
141. /**
142. * 把数据对象转换成json字符串
143. * DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
144. * 数组对象形如:[{}, {}, {}, ...]
145. * map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...}
146. * @param object
147. * @return
148. */
149. public static String getJSONString(Object object) throws
150. null;
151. //日期值处理器
152. new
153. java.util.Date.class, new
154. if(object != null){
155. if(object instanceof Collection || object instanceof
156. jsonString = JSONArray.fromObject(object, jsonConfig).toString();
157. else{
158. jsonString = JSONObject.fromObject(object, jsonConfig).toString();
159. }
160. }
161. return jsonString == null ? "{}"
162. }