json-lib这个java类包用于把bean,map和xml转换成json并能够把json转回成bean和dynabean。
下载地址:
还要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、list
- boolean[] boolarray = newboolean[]{true,false,true};
- jsonarray jsonarray1 = jsonarray.fromobject( boolarray );
- system.out.println( jsonarray1 );
-
-
- list list = new arraylist();
- list.add( "first" );
- list.add( "second" );
- jsonarray jsonarray2 = jsonarray.fromobject( list );
- system.out.println( jsonarray2 );
-
-
- jsonarray jsonarray3 = jsonarray.fromobject( "['json','is','easy']" );
- system.out.println( jsonarray3 );
-
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
- 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 );
-
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
-
-
-
-
-
-
-
-
- jsonobject jsonobject = jsonobject.fromobject( new jsonbean() );
- system.out.println( jsonobject );
-
/**
* 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
-
-
-
-
-
-
- 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() );
-
/**
* 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
- 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" ) );
- 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)
jsonobject json = new jsonobject( true );
string xml = xmlserializer.write( json );
2)
jsonobject json = jsonobject.fromobject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
string xml = xmlserializer.write( json );
json
true
1
json
true
1
3)
jsonarray json = jsonarray.fromobject("[1,2,3]");
string xml = xmlserializer.write( json );
7 、xml to json
jsonarray json = (jsonarray) xmlserializer.read( xml );
system.out.println( json );
// prints [function(i,j){ return matrix[i][j]; }]
出自