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

JSON与JAVA的数据转换--String->Bean
5、String to bean
    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 );       
     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 eXP ected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );       
     assertEquals( exp ected, (List) PropertyUtils.getProperty( bean, "array" ) );      
 
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 );     
  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 exp ected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );     
  assertEquals( exp ected, (List) PropertyUtils.getProperty( bean, "array" ) );     
 
  String json = "{"value":"xx","row":1,"col":1}";       
  JSONObject jsonObject = JSONObject.fromString(json);    
  JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );       
      assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );       
      assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );       
      assertEquals( jsonObject.get( "value" ), bean.getValue() );      
 
String json = "{"value":"xx","row":1,"col":1}";     
JSONObject jsonObject = JSONObject.fromString(json);  
JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );     
    assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );     
    assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );     
    assertEquals( jsonObject.get( "value" ), bean.getValue() );     
1 楼 gqzyyxh 2010-10-28  
你好:
JSONObject jsonObject = JSONObject.fromString(json)
这段代码里面fromString(String)方法,需要哪个jar包呢,
我用的是json-lib-2.2.3-jdk15.jar和ezmorph-1.0.4.jar这两个jar包,
只有fromObject(String)这个方法,谢谢~
2 楼 jianxia801 2012-04-17  
现在天下文章一大抄;实际没有这个fromString方法:准确的方法是 jsonObject = JSONObject.fromObject(json);
    Student stu = (Student) JSONObject.toBean(jsonObject, Student.class);
    fail(stu.toString());
参考文章:http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html 为以后寻找此转换的人 提供一个便捷地址。