`

JSON与JAVA的数据转换

 
阅读更多
  1. Map map = new HashMap();       
  2. map.put( "name""json" );       
  3. map.put( "bool", Boolean.TRUE );       
  4.     
  5. map.put( "int"new Integer(1) );       
  6. map.put( "arr"new String[]{"a","b"} );       
  7. map.put( "func""function(i){ return this.arr[i]; }" );       
  8. JSONObject json = JSONObject.fromObject( map );       
  9. System.out.println( json );       
  10.  //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}    
  11.  
  12. Map map = new HashMap();     
  13.  map.put( "name""json" );     
  14.  map.put( "bool", Boolean.TRUE );     
  15.    
  16.  map.put( "int"new Integer(1) );     
  17.  map.put( "arr"new String[]{"a","b"} );     
  18.  map.put( "func""function(i){ return this.arr[i]; }" );     
  19.  JSONObject json = JSONObject.fromObject( map );     
  20.  System.out.println( json );     
  21.  //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true} 

BEAN

 

  1. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );       
  2.    System.out.println( jsonObject );       
  3.    //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}      
  4.  
  5.  
  6. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );     
  7. System.out.println( jsonObject );     
  8.  //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}   

 BEANS

 

 

 
  1. List list = new ArrayList();    
  2.         JsonBean2 jb1 = new JsonBean2();    
  3.         jb1.setCol(1);    
  4.         jb1.setRow(1);    
  5.         jb1.setValue("xx");    
  6.             
  7.         JsonBean2 jb2 = new JsonBean2();    
  8.         jb2.setCol(2);    
  9.         jb2.setRow(2);    
  10.         jb2.setValue("");    
  11.              
  12.         list.add(jb1);    
  13.         list.add(jb2);    
  14.             
  15.         JSONArray ja = JSONArray.fromObject(list);    
  16.         System.out.println( ja.toString() );    
  17.         //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]    
  18.  
  19.     
  20. List list = new ArrayList();  
  21.         JsonBean2 jb1 = new JsonBean2();  
  22.         jb1.setCol(1);  
  23.         jb1.setRow(1);  
  24.         jb1.setValue("xx");  
  25.           
  26.         JsonBean2 jb2 = new JsonBean2();  
  27.         jb2.setCol(2);  
  28.         jb2.setRow(2);  
  29.         jb2.setValue("");  
  30.           
  31.           
  32.         list.add(jb1);  
  33.         list.add(jb2);  
  34.           
  35.         JSONArray ja = JSONArray.fromObject(list);  
  36.         System.out.println( ja.toString() );  
  37.         //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]   

 String to bean

 

  1.   String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";       
  2.    JSONObject jsonObject = JSONObject.fromString(json);       
  3.    Object bean = JSONObject.toBean( jsonObject );       
  4.      assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );       
  5.      assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );       
  6.      assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );       
  7.      assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );       
  8.      assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );       
  9.      List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );       
  10.      assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );      
  11.  
  12. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";     
  13. JSONObject jsonObject = JSONObject.fromString(json);     
  14. Object bean = JSONObject.toBean( jsonObject );     
  15.   assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );     
  16.   assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );     
  17.   assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );     
  18.   assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );     
  19.   assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );     
  20.   List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );     
  21.   assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );     

 

  1. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";       
  2.   JSONObject jsonObject = JSONObject.fromString(json);    
  3.   JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );       
  4.       assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );       
  5.       assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );       
  6.       assertEquals( jsonObject.get( "value" ), bean.getValue() );      
  7.  
  8. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";     
  9. JSONObject jsonObject = JSONObject.fromString(json);  
  10. JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );     
  11.     assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );     
  12.     assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );     
  13.     assertEquals( jsonObject.get( "value" ), bean.getValue() );     

 

json to xml

1)

  1. JSONObject json = new JSONObject( true );  
  2. String xml = XMLSerializer.write( json ); 
  1. < o class="object" null="true"> 

2)

  1. JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");  
  2. String xml = XMLSerializer.write( json ); 

 

  1. < o class="object"> 
  2.    < name type="string">json< /name> 
  3.    < bool type="boolean">true< /bool> 
  4.    < int type="number">1< /int> 
  5. < /o> 
  6.    < o class="object"> 
  7.       < name type="string">json< /name> 
  8.       < bool type="boolean">true< /bool> 
  9.       < int type="number">1< /int> 
  10.    < /o> 

3)

  1. JSONArray json = JSONArray.fromObject("[1,2,3]");  
  2. String xml = XMLSerializer.write( json ); 

 

  1. < a class="array"> 
  2.    < e type="number">1< /e> 
  3.    < e type="number">2< /e> 
  4.    < e type="number">3< /e> 
  5. < /a> 

7 、xml to json

  1. < a class="array"> 
  2.   < e type="function" params="i,j"> 
  3.       return matrix[i][j];  
  4.   < /e> 
  5. < /a> 
  6.    < a class="array"> 
  7.      < e type="function" params="i,j"> 
  8.          return matrix[i][j];  
  9.      < /e> 
  10.    < /a> 
  1. JSONArray json = (JSONArray) XMLSerializer.read( xml );  
  2. System.out.println( json );  

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics