首页 > 代码库 > 如何使用Json-lib

如何使用Json-lib

  1. 数组与List、Collection等都用JSONArray解析
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"]
2. 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];}} */

 

3.From JSON to Beans

 

Convert to DynaBean:

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" ) );

 

Convert to Bean:

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() );

 

Working with XML
From JSON to XML
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");  String xml = XMLSerializer.write( json );

 

From XML to JSON
JSONArray json = (JSONArray) XMLSerializer.read( xml );  System.out.println( json );  // prints [function(i,j){ return matrix[i][j]; }]

如何使用Json-lib