日期:2014-05-16  浏览次数:20532 次

JSON与javabean对象互换
[url]http://code.google.com/p/google-gson/ [/url]



http://json-lib.sourceforge.net/index.html



json-lib使用了ezmorph进行对象之间的转换,已经实现了基本类型的转换支持。基本使用上手较快。

需要下面JAR 包

jakarta commons-lang 2.4
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1.1



boolean[] boolArray = new boolean[]{true,false,true};   
JSONArray jsonArray = JSONArray.fromObject( boolArray );   
System.out.println( jsonArray );   
// prints [true,false,true]  

 
List list = new ArrayList();      
list.add( "first" );      
list.add( "second" );      
JSONArray jsonArray = JSONArray.fromObject( list );      
System.out.println( jsonArray );      
// prints ["first","second"]   


 
  
JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );   
System.out.println( jsonArray );   
// prints ["json","is","easy"]  


From Beans & Maps to 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 jsonObject = JSONObject.fromObject( map );      
System.out.println( jsonObject );      
// prints ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){ return this.arr[i]; }]   

 
 
class MyBean{      
   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];");      
     
   // getters & setters      
   ...      
}      
     
JSONObject jsonObject = JSONObject.fromObject( new MyBean() );      
System.out.println( jsonObject );      
/* prints     
  {"name":"json","pojoId":1,"options":["a","f"],     
  "func1":function(i){ return this.options[i];},     
  "func2":function(i){ return this.options[i];}}   


 



From JSON to Beans

 String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";      
JSONObject jsonObject = JSONObject.fromObject( 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" ) );      
Assertions.assertListEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );  
 

  
 String json = "{bool:true,integer:1,string:\"json\"}";      
JSONObject jsonObject = JSONObject.fromObject( json );      
BeanA bean = (BeanA) JSONObject.toBean( jsonObject, BeanA.class );      
assertEquals( jsonObject.get( "bool" ), Boolean.valueOf( bean.isBool() ) );      
assertEquals( jsonObject.get( "integer" ), new Integer( bean.getInteger() ) );      
assertEquals( jsonObject.get( "string" ), bean.getString() ); 
  

 



gson 提供了一个JSON <-->Java