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

JSON与JAVA的数据转换--Bean
3、BEAN
        
   JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );       
   System.out.println( jsonObject );       
   //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}      
 
 
JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );     
System.out.println( jsonObject );     
 //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}     
 
  4、BEANS
      
List list = new ArrayList();    
        JsonBean2 jb1 = new JsonBean2();    
        jb1.setCol(1);    
        jb1.setRow(1);    
        jb1.setValue("xx");    
            
        JsonBean2 jb2 = new JsonBean2();    
        jb2.setCol(2);    
        jb2.setRow(2);    
        jb2.setValue("");    
             
        list.add(jb1);    
        list.add(jb2);    
            
        JSONArray ja = JSONArray.fromObject(list);    
        System.out.println( ja.toString() );    
        //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]    
 
    
List list = new ArrayList();  
        JsonBean2 jb1 = new JsonBean2();  
        jb1.setCol(1);  
        jb1.setRow(1);  
        jb1.setValue("xx");  
          
        JsonBean2 jb2 = new JsonBean2();  
        jb2.setCol(2);  
        jb2.setRow(2);  
        jb2.setValue("");  
          
          
        list.add(jb1);  
        list.add(jb2);  
          
        JSONArray ja = JSONArray.fromObject(list);  
        System.out.println( ja.toString() );  
        //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]