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