zoukankan      html  css  js  c++  java
  • 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();
        }
        
    }
    
  • 相关阅读:
    个人永久性免费-Excel催化剂功能第31波-数量金额分组凑数功能,财务表哥表姐最爱
    个人永久性免费-Excel催化剂功能第30波-工作表快捷操作(批量创建、命名、排序、工作表目录)
    个人永久性免费-Excel催化剂功能第29波-追加中国特色的中文相关自定义函数
    发现用System.Net.Mail发邮件(代码附后),附件稍微大一点就会造成程序假死. 有没有什么简单的解决办法呢? 多谢!!
    上传文件 获取文件名 --360浏览器
    js中的json对象和字符串之间的转化
    chosen.jquery.js
    select 后台获取text 并根据text 设置value选中项
    iframe中有ajax,设置iframe自适应高度
    charme浏览器 jquery1.9.1min.js 报脚本错误 无jquery.min.map 文件
  • 原文地址:https://www.cnblogs.com/dingding0505/p/3336441.html
Copyright © 2011-2022 走看看