json 和 bean 转换 (小例子2)

来源:互联网 发布:新开的淘宝店怎么升级 编辑:程序博客网 时间:2024/06/05 17:22
Java代码 复制代码 收藏代码
  1. package com.testjson; 
  2.  
  3. public class Person 
  4.     private String name; 
  5.      
  6.     private Integer age; 
  7.      
  8.     public String getName() 
  9.     { 
  10.         return name; 
  11.     } 
  12.      
  13.     public void setName(String name) 
  14.     { 
  15.         this.name = name; 
  16.     } 
  17.      
  18.     public Integer getAge() 
  19.     { 
  20.         return age; 
  21.     } 
  22.      
  23.     public void setAge(Integer age) 
  24.     { 
  25.         this.age = age; 
  26.     } 
  27.      

Java代码 复制代码 收藏代码
  1. package com.testjson; 
  2.  
  3. import net.sf.json.JSONObject; 
  4.  
  5. public class test 
  6.     public staticvoid main(String[] args) 
  7.     { 
  8.         Person p = new Person(); 
  9.         p.setAge(11); 
  10.         p.setName("测试"); 
  11.          
  12.         JSONObject jsonObj1 = JSONObject.fromObject(p); 
  13.          
  14.         System.out.println("JSON输出后的:" + jsonObj1.toString()); 
  15.          
  16.         JSONObject jsonObj2 = JSONObject.fromObject(jsonObj1); 
  17.          
  18.         System.out.println("JSONObject输出后的:" + jsonObj2.toString()); 
  19.          
  20.         Person p2 = (Person)JSONObject.toBean(jsonObj1, Person.class); 
  21.          
  22.         System.out.println("json转化为对象:姓名:" + p2.getName() +";年龄:" + p2.getAge()); 
  23.          
  24.         /*********处理js格式问题************/ 
  25.         //        JsonConfig config = new JsonConfig(); 
  26.         //        config.setIgnoreDefaultExcludes(false); 
  27.         //        config.registerJsonBeanProcessor(Date.class, new JsDateJsonBeanProcessor()); 
  28.         /**************处理Integer为null时输出为0的问题 版本需要2.3**************/ 
  29.         //        JsonConfig jsonConfig = new JsonConfig(); 
  30.         //        jsonConfig.registerDefaultValueProcessor(Integer.class, new MyDefaultIntegerValueProcessor()); 
  31.         //        JsonConfig jsonConfig = new JsonConfig(); 
  32.         //        jsonConfig.findJsonValueProcessor(Integer.class, new DefaultValueProcessor() 
  33.         //        { 
  34.         //            public Object getDefaultValue(Class type) 
  35.         //            { 
  36.         //                return null; 
  37.         //            } 
  38.         //        }); 
  39.          
  40.     } 

Java代码 复制代码 收藏代码
  1. package com.testjson; 
  2.  
  3. import net.sf.json.JSONNull; 
  4. import net.sf.json.processors.DefaultValueProcessor; 
  5.  
  6. public class MyDefaultIntegerValueProcessorimplements DefaultValueProcessor 
  7.      
  8.     public Object getDefaultValue(Class type) 
  9.     { 
  10.         if (type != null && Integer.class.isAssignableFrom(type)) 
  11.         { 
  12.             return Integer.valueOf(9999); 
  13.         } 
  14.         return JSONNull.getInstance(); 
  15.     } 
  16.      
0 0
原创粉丝点击