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

JSON与JAVA的数据转换--Map
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 );       
 //{"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}