json和bean转换(转)

 

 

Java代码 复制代码 收藏代码
  1. package com.testjson;   
  2.   
  3. public class Person   
  4. {   
  5.     private String name;   
  6.        
  7.     private Integer age;   
  8.        
  9.     public String getName()   
  10.     {   
  11.         return name;   
  12.     }   
  13.        
  14.     public void setName(String name)   
  15.     {   
  16.         this.name = name;   
  17.     }   
  18.        
  19.     public Integer getAge()   
  20.     {   
  21.         return age;   
  22.     }   
  23.        
  24.     public void setAge(Integer age)   
  25.     {   
  26.         this.age = age;   
  27.     }   
  28.        
  29. }  
package com.testjson;

public class Person
{
    private String name;
    
    private Integer age;
    
    public String getName()
    {
        return name;
    }
    
    public void setName(String name)
    {
        this.name = name;
    }
    
    public Integer getAge()
    {
        return age;
    }
    
    public void setAge(Integer age)
    {
        this.age = age;
    }
    
}

 

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

import net.sf.json.JSONObject;

public class test
{
    public static void main(String[] args)
    {
        Person p = new Person();
        p.setAge(11);
        p.setName("测试");
        
        JSONObject jsonObj1 = JSONObject.fromObject(p);
        
        System.out.println("JSON输出后的:" + jsonObj1.toString());
        
        JSONObject jsonObj2 = JSONObject.fromObject(jsonObj1);
        
        System.out.println("JSONObject输出后的:" + jsonObj2.toString());
        
        Person p2 = (Person)JSONObject.toBean(jsonObj1, Person.class);
        
        System.out.println("json转化为对象:姓名:" + p2.getName() + ";年龄:" + p2.getAge());
        
        /*********处理js格式问题************/
        //        JsonConfig config = new JsonConfig();
        //        config.setIgnoreDefaultExcludes(false);
        //        config.registerJsonBeanProcessor(Date.class, new JsDateJsonBeanProcessor());
        /**************处理Integer为null时输出为0的问题 版本需要2.3**************/
        //        JsonConfig jsonConfig = new JsonConfig();
        //        jsonConfig.registerDefaultValueProcessor(Integer.class, new MyDefaultIntegerValueProcessor());
        //        JsonConfig jsonConfig = new JsonConfig();
        //        jsonConfig.findJsonValueProcessor(Integer.class, new DefaultValueProcessor()
        //        {
        //            public Object getDefaultValue(Class type)
        //            {
        //                return null;
        //            }
        //        });
        
    }
}

 

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

import net.sf.json.JSONNull;
import net.sf.json.processors.DefaultValueProcessor;

public class MyDefaultIntegerValueProcessor implements DefaultValueProcessor
{
    
    public Object getDefaultValue(Class type)
    {
        if (type != null && Integer.class.isAssignableFrom(type))
        {
            return Integer.valueOf(9999);
        }
        return JSONNull.getInstance();
    }
    
}

 

 

posted on 2013-09-24 10:47  TouTou9055  阅读(451)  评论(0编辑  收藏  举报

导航