JSON与JAVA数据的转换(二)

长平狐 发布于 2012/11/28 15:41
阅读 76
收藏 0
5、String to bean 
Java代码
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";      
JSONObject jsonObject = JSONObject.fromString(json);      
Object bean = JSONObject.toBean( jsonObject );         

Java代码
String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";      
JSONObject jsonObject = JSONObject.fromString(json);   
   JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );      

6 json to xml 
1) 
JSONObject json = new JSONObject( true ); 
String xml = XMLSerializer.write( json ); 

<o class="object" null="true"> 

2) 
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}"); 
String xml = XMLSerializer.write( json ); 
<o class="object"> 
<name type="string">json</name> 
<bool type="boolean">true</bool> 
<int type="number">1</int> 
</o> 
<o class="object"> 
<name type="string">json</name> 
<bool type="boolean">true</bool> 
<int type="number">1</int> 
</o> 
3) 
JSONArray json = JSONArray.fromObject("[1,2,3]"); 
String xml = XMLSerializer.write( json ); 
<a class="array"> 
<e type="number">1</e> 
<e type="number">2</e> 
<e type="number">3</e> 
</a> 

7 、xml to json 
<a class="array"> 
<e type="function" params="i,j"> 
return matrix[i][j]; 
</e> 
</a> 
<a class="array"> 
<e type="function" params="i,j"> 
return matrix[i][j]; 
</e> 
</a> 

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



原文链接:http://blog.csdn.net/longeremmy/article/details/8089803
加载中
返回顶部
顶部