转:JSON与JAVA数据的转换
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。

下载地址:json-lib.sourceforge/
还要需要的第3方包:
org.apachemons(3.2以上版本)

ph(ezmorph-1.0.4.jar)
nu.xom

1、List
Java代码
1. boolean[] boolArray = newboolean[]{true,false,true};
2. JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
3. System.out.println( jsonArray1 );
4. // prints [true,false,true]
5.
6. List list = newArrayList();
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"]
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代码
1. Map map = newHashMap();
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. JSONObjectjson = 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}
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代码
1. /**
2. * Bean.java
3. private String name = "json";
4. private intpojoId = 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. JSONObjectjsonObject = JSONObject.fromObject( newJsonBean() );
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]; }}
/**
* 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代码
1. /**
2. json转换对象* private int row ;
3. private int col ;
4. private String value ;
5. *
6. */
7. List list = newArrayList();
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. JSONArrayja = JSONArray.fromObject(list);
23. System.out.println( ja.toString() );
24. //[{"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代码
1. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
2. JSONObjectjsonObject = JSONObject.fromString(json);
3. Object bean = Bean( jsonObject );
4. assertEquals( ( "name" ), Property( bean, "name" ) );

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。