首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JSON与javabean对象交换

2012-12-22 
JSON与javabean对象互换[url]http://code.google.com/p/google-gson/ [/url] http://json-lib.sourceforge

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 <-->JavaBean相互转换的框架,采用了java中的reflect,同时类型转换要求比较严格, 如果是图方便最好还是用json-lib。第一眼就没看出来怎么用,后来才发现:



Gson是google自己写的一个Java对象与JSON相互转化的工具包.它轻巧简便,易于使用,而且有很完备的文档可供查询,不用多说,当然是开源喽.

List<List<Map<String, String>>>类型的java对象obj转化为json的格式,你可以这么写 Gson gson = new Gson;  
        
String json = gson.toJson(obj, new TypeToken<List<List<Map<String, String>>>(){}.getType());// to Json            List<List<Map<String, String>>> obj2 = gson.from(obj, new TypeToken<List<List<Map<String, String>>>(){}.getType());// from json  
 

Gson这个类是其中的关键,它负责来进行转化java对象和json.将对象转化为json的格式,使用方法toJson(),这个方法有几个不同的用法.对于比较简单的对象,比如一个数组,或者一个list,你可以只将要转化的java对象作为参数,可以如果你的对象的格式蛮复杂,那么,你就需要另一个参数来描述一个java对象的结构,这另一个参数可以是Type或者是Class,它的建立方法也很简单

热点排行
Bad Request.