json-lib.jar开发包使用:
依赖包:
commons-beanutils.jar;
commons-httpclient.jar;
commons-lang.jar;
ezmorph.jar;不少人使用时会提示net.sf.ezmorph.xxx找不到,就是缺这个:
morph-1.0.1.jar
相关链接:
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
使用过程中问题:
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 java.util.ArrayList;
2.import java.util.HashMap;
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 Json {
10. public static void main(String[] args) {
11. Json j = new Json();
12. j.bean2json();
13. }
14.
15. public void arr2json() {
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 list2json() {
23. List list = new ArrayList();
24. list.add("first");
25. list.add("second");
26. JSONArray jsonArray = JSONArray.fromObject(list);
27. System.out.println(jsonArray);
28. // prints ["first","second"]
29. }
30.
31. public void createJson() {
32. JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
33. System.out.println(jsonArray);
34. // prints ["json","is","easy"]
35. }
36.
37. public void map2json() {
38. Map
39. map.put("name", "json");
40. map.put("bool", Boolean.TRUE);
41. map.put("int", new Integer(1));
42. map.put("arr", new String[] { "a", "b" });
43. map.put("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 bean2json() {
53. JSONObject jsonObject = JSONObject.fromObject(new MyBean());
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 json2bean() {
64. String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
65. JSONObject jb = JSONObject.fromString(json);
66. JSONObject.toBean(jb, MyBean.class);
67. System.out.println();
68. }
69.}
操作的bean:
1.import net.sf.json.JSONFunction;
2.
3.public class MyBean {
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的方法
JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。本文将快速讲解 JSON 格式,并通过代码示例演示如何分别在客户端和服务器端进行 JSON 格式数据的处理。
Json必需的包
commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
json-lib-2.2.3-jdk13.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar
以上包可以从
http://commons.apache.org/index.html
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
http://www.docjar.com/
中下载到。
出现java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher错误是因为没有导入ezmorph.jar文件或版本不对。
出现java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap错误是因为没有导入commons-collections.jar文件或版本不对。
Java代码转换成json代码
1. List集合转换成json代码
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合转换成json代码
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
3. Bean转换成json代码
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 数组转换成json代码
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般数据转换成json代码
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
6. beans转换成json代码
List list = new ArrayList();
JsonBean2 jb1 = new JsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");
JsonBean2 jb2 = new JsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");
list.add(jb1);
list.add(jb2);
JSONArray ja = JSONArray.fromObject(list);