SON 即 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);
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
下载地址:http://json-lib.sourceforge.net/ 还要需要的第3方包:
Java代码
1. org.apache.commons(3.2以上版本)
2. org.apache.oro
3. net.sf.ezmorph(ezmorph-1.0.4.jar)
4. nu.xom
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、List
Java代码
Java代码
1. boolean[] boolArray = new boolean[]{true,false,true};
2. JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
3. System.out.println( jsonArray1 );
4. // prints [true,false,true]
5.
6. List list = new ArrayList();
7. list.add( "first" );
8. list.add( "second" );
9. JSONArray jsonArray2 = JSONArray.fromObject( list );
10. System.out.println( jsonArray2 );
11. // prints ["first","second"]
12.
13. JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
14. System.out.println( jsonArray3 );
15. // prints ["json","is","easy"]
16. boolean[] boolArray = new boolean[]{true,false,true};
17. JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
18. System.out.println( jsonArray1 );
19. // prints [true,false,true]
20. List list = new ArrayList();
21. list.add( "first" );
22. list.add( "second" );
23. JSONArray jsonArray2 = JSONArray.fromObject( list );
24. System.out.println( jsonArray2 );
25. // prints ["first","second"]
26. JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
27. System.out.println( jsonArray3 );
28. // prints ["json","is","easy"]
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
System.out.println( jsonArray1 );
// prints [true,false,true]
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
System.out.println( jsonArray2 );
// prints ["first","second"]
JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
System.out.println( jsonArray3 );
// prints ["json","is","easy"]
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
System.out.println( jsonArray1 );
// prints [true,false,true]
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
System.out.println( jsonArray2 );
// prints ["first","second"]
JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
System.out.println( jsonArray3 );
// prints ["json","is","easy"]
2、Map
Java代码
Java代码
1. Map map = new HashMap();
2. map.put( "name", "json" );
3. map.put( "bool", Boolean.TRUE );
4.
5. map.put( "int", new Integer(1) );
6. map.put( "arr", new String[]{"a","b"} );
7. map.put( "func", "function(i){ return this.arr[i]; }" );
8. JSONObject json = JSONObject.fromObject( map );
9. System.out.println( json );
10. //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
11. Map map = new HashMap();
12. map.put( "name", "json" );
13. map.put( "bool", Boolean.TRUE );
14. map.put( "int", new Integer(1) );
15. map.put( "arr", new String[]{"a","b"} );
16. map.put( "func", "function(i){ return this.arr[i]; }" );
17. JSONObject json = JSONObject.fromObject( map );
18. System.out.println( json );
19. //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
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 );
System.out.println( json );
//{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
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 );
System.out.println( json );
//{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
3、BEAN
Java代码
Java代码
1. /**
2. * Bean.java
3. private String name = "json";
4. private int pojoId = 1;
5. private char[] options = new char[]{'a','f'};
6. private String func1 = "function(i){ return this.options[i]; }";
7. private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
8. */
9. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
10. System.out.println( jsonObject );
11. //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
12. /**
13. * Bean.java
14. private String name = "json";
15. private int pojoId = 1;
16. private char[] options = new char[]{'a','f'};
17. private String func1 = "function(i){ return this.options[i]; }";
18. private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
19. */
20. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
21. System.out.println( jsonObject );
22. //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
/**
* Bean.java
private String name = "json";
private int pojoId = 1;
private char[] options = new char[]{'a','f'};
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
*/
JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
System.out.println( jsonObject );
//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
/**
* Bean.java
private String name = "json";
private int pojoId = 1;
private char[] options = new char[]{'a','f'};
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
*/
JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );
System.out.println( jsonObject );
//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
4、BEANS
Java代码
Java代码
1. /**
2. * private int row ;
3. private int col ;
4. private String value ;
5. *
6. */
7. List list = new ArrayList();
8. JsonBean2 jb1 = new JsonBean2();
9. jb1.setCol(1);
10. jb1.setRow(1);
11. jb1.setValue("xx");
12.
13. JsonBean2 jb2 = new JsonBean2();
14. jb2.setCol(2);
15. jb2.setRow(2);
16. jb2.setValue("");
17.
18.
19. list.add(jb1);
20. list.add(jb2);
21.
22. JSONArray ja = JSONArray.fromObject(list);
23. System.out.println( ja.toString() );
24. //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
25. /**
26. * private int row ;
27. private int col ;
28. private String value ;
29. *
30. */
31. List list = new ArrayList();
32. JsonBean2 jb1 = new JsonBean2();
33. jb1.setCol(1);
34. jb1.setRow(1);
35. jb1.setValue("xx");
36. JsonBean2 jb2 = new JsonBean2();
37. jb2.setCol(2);
38. jb2.setRow(2);
39. jb2.setValue("");
40. list.add(jb1);
41. list.add(jb2);
42. JSONArray ja = JSONArray.fromObject(list);
43. System.out.println( ja.toString() );
44. //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
/**
* private int row ;
private int col ;
private String value ;
*
*/
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);
System.out.println( ja.toString() );
//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
/**
* private int row ;
private int col ;
private String value ;
*
*/
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);
System.out.println( ja.toString() );
//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
5、String to bean
Java代码
Java代码
1. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
2. JSONObject jsonObject = JSONObject.fromString(json);
3. Object bean = JSONObject.toBean( jsonObject );
4. assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
5. assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
6. assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
7. assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
8. assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
9. List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
10. assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
11. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
12. JSONObject jsonObject = JSONObject.fromString(json);
13. Object bean = JSONObject.toBean( jsonObject );
14. assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
15. assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
16. assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
17. assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
18. assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
19. List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
20. assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromString(json);
Object bean = JSONObject.toBean( jsonObject );
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromString(json);
Object bean = JSONObject.toBean( jsonObject );
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
Java代码
Java代码
1. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
2. JSONObject jsonObject = JSONObject.fromString(json);
3. JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
4. assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
5. assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
6. assertEquals( jsonObject.get( "value" ), bean.getValue() );
7. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
8. JSONObject jsonObject = JSONObject.fromString(json);
9. JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
10. assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
11. assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
12. assertEquals( jsonObject.get( "value" ), bean.getValue() );
String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
JSONObject jsonObject = JSONObject.fromString(json);
JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
assertEquals( jsonObject.get( "value" ), bean.getValue() );
String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";
JSONObject jsonObject = JSONObject.fromString(json);
JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );
assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );
assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );
assertEquals( jsonObject.get( "value" ), bean.getValue() );
6 json to xml
1)
Java代码
1. JSONObject json = new JSONObject( true );
2. String xml = XMLSerializer.write( json );
3.
4. <o class="object" null="true">
JSONObject json = new JSONObject( true );
String xml = XMLSerializer.write( json );
<o class="object" null="true">
2)
Java代码
1. JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
2. String xml = XMLSerializer.write( json );
3. <o class="object">
4. <name type="string">json</name>
5. <bool type="boolean">true</bool>
6. <int type="number">1</int>
7. </o>
8. <o class="object">
9. <name type="string">json</name>
10. <bool type="boolean">true</bool>
11. <int type="number">1</int>
12. </o>
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
String xml = XMLSerializer.write( json );
<o class="object">
<name type="string">json</name>
<bool type="boolean">true</bool>
<int type="number">1</int>
</o>
<o class="object">
<name type="string">json</name>
<bool type="boolean">true</bool>
<int type="number">1</int>
</o>
3)
Java代码
1. JSONArray json = JSONArray.fromObject("[1,2,3]");
2. String xml = XMLSerializer.write( json );
3. <a class="array">
4. <e type="number">1</e>
5. <e type="number">2</e>
6. <e type="number">3</e>
7. </a>
JSONArray json = JSONArray.fromObject("[1,2,3]");
String xml = XMLSerializer.write( json );
<a class="array">
<e type="number">1</e>
<e type="number">2</e>
<e type="number">3</e>
</a>
7 、xml to json
Java代码
1. <a class="array">
2. <e type="function" params="i,j">
3. return matrix[i][j];
4. </e>
5. </a>
6. <a class="array">
7. <e type="function" params="i,j">
8. return matrix[i][j];
9. </e>
10. </a>
<a class="array">
<e type="function" params="i,j">
return matrix[i][j];
</e>
</a>
<a class="array">
<e type="function" params="i,j">
return matrix[i][j];
</e>
</a>
Java代码
1. JSONArray json = (JSONArray) XMLSerializer.read( xml );
2. System.out.println( json );
3. // prints [function(i,j){ return matrix[i][j]; }]