码迷,mamicode.com
首页 > Web开发 > 详细

json转换成对象

时间:2016-05-16 19:24:18      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

在json转换成对象时,json的key会与java 类的字段一一对应。如果没有映射上的java字段会在该数据类型上填充默认值,如int 0,String null 等。

没有映射的json key在程序结束后会用警告显示出来,告知程序员那个key值没有被映射上

 

 

技术分享
import net.sf.json.JSONObject;

public class MainClass {

    public static void main(String[] args) {
        TestJsonBean();
        
    }

    
    
    /**
     * java对象与json对象互相转换
     */
    private static void TestJsonBean() {
        /**
         * 创建java对象
         */
        Student student = new Student();
        student.setId(1);
        student.setName("jag");
        student.setSex("man");
        student.setAge(25);
        student.setHobby(new String[]{"篮球","上网","跑步","游戏"});
        /**
         * java对象转换成json对象,并获取json对象属性
         */
        JSONObject jsonStu = JSONObject.fromObject(student);
        System.out.println(jsonStu.toString());
        System.out.println(jsonStu.getJSONArray("hobby"));
        /**
         * json对象转换成java对象,并获取java对象属性
         */
        Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);
        System.out.println(stu.getName());
        /**
         * 创建json对象
         */
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("id",1);
        jsonObj.put("name","张勇");
        jsonObj.put("sex","男");
        //jsonObj.put("age",24);
        //jsonObj.put("hobby",new String[]{"上网","游戏","跑步","音乐"});
        //System.out.println(jsonObj.toString());
        /**
         * json对象转换成java对象
         */
        Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
        System.out.println(stud.getAge());        
    }
}
View Code

运行结果

{"age":25,"hobby":["篮球","上网","跑步","游戏"],"id":1,"name":"jag","sex":"man"}
["篮球","上网","跑步","游戏"]
jag
0

改一下代码

JSONObject jsonObj = new JSONObject();
        jsonObj.put("id",1);
        jsonObj.put("name","张勇");
        jsonObj.put("old","男");//这个是类没有的元素

结果;

{"age":25,"hobby":["篮球","上网","跑步","游戏"],"id":1,"name":"jag","sex":"man"}
["篮球","上网","跑步","游戏"]
jag
0
五月 16, 2016 5:18:22 下午 net.sf.json.JSONObject toBean
警告: Tried to assign property old:java.lang.String to bean of class Student

student 类

技术分享
import java.io.Serializable;

public class Student implements Serializable {

    private long id;
    private String name;
    private String sex;
    private int age;
    private String[] hobby;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String[] getHobby() {
        return hobby;
    }
    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }
    
}
View Code

 

json转换成对象

标签:

原文地址:http://www.cnblogs.com/zhongzheng123/p/5498780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!