java中json数据的生成以及解析

来源:互联网 发布:宋江 知乎 编辑:程序博客网 时间:2024/05/19 21:17

一.json理解

1.json(JavaScript Object Notation):javascript对象表示法(采用javascript对象的语法来表示对象数据),它是一种存储和传输数据的格式,它的优点是易读,易解析,相比起传统的xml来说,它具有更小,更快,易解析的优点。—更详细请参看W3C

二.利用java生成json数据的方式

1.使用org.json jar包中的json API中的JSONObject类
    jar包的maven依赖如下:

        <dependency>            <groupId>org.json</groupId>            <artifactId>json</artifactId>            <version>20160810</version>        </dependency>

java代码如下:

        Object nullObject = null;        JSONObject wangxiaoer = new JSONObject();        wangxiaoer.put("name", "王小二");        wangxiaoer.put("age", 25.2);        wangxiaoer.put("birthday", "1990-01-01");        wangxiaoer.put("school", "蓝翔");        wangxiaoer.put("major", new String[] { "理发", "挖掘机" });        wangxiaoer.put("has_girlfriend", false);        wangxiaoer.put("house", nullObject);        wangxiaoer.put("car", nullObject);        wangxiaoer.put("comment", "这是一个注释");        System.out.println(wangxiaoer.toString());

eclipse中的运行效果截图如下:
这里写图片描述

经过json在线编辑器格式化以后效果如下所示:
这里写图片描述

2.使用hashmap来生成json数据,这种方式会使用JSONObject类的一个构造方法new JSONObject(),此构造方法的入参为Map类型的接口
java代码如下:

        Map<String,Object> wangxiaoer=new HashMap<String,Object>();        Object nullObject = null;        wangxiaoer.put("name", "王小二");        wangxiaoer.put("age", 25.2);        wangxiaoer.put("birthday", "1990-01-01");        wangxiaoer.put("school", "蓝翔");        wangxiaoer.put("major", new String[] { "理发", "挖掘机" });        wangxiaoer.put("has_girlfriend", false);        wangxiaoer.put("house", nullObject);        wangxiaoer.put("car", nullObject);        wangxiaoer.put("comment", "这是一个注释");        System.out.println(new JSONObject(wangxiaoer).toString());

最终eclipse中的运行效果同上。

3.使用javabean来生成json数据,这种方式也会使用JSONObject类的一个构造方法new JSONObject(),此构造方法的入参为javabean类型的实例
代码如下:

        DiaoSi wangxiaoer=new DiaoSi();        wangxiaoer.setName("王小二");        wangxiaoer.setAge(25.2);        wangxiaoer.setBirthday("1990-01-01");        wangxiaoer.setSchool("蓝翔");        wangxiaoer.setMajor(new String[] { "理发", "挖掘机" });        wangxiaoer.setHas_girlfriend(false);        wangxiaoer.setCar(null);        wangxiaoer.setHouse(null);        wangxiaoer.setComment("这是一个注释");        System.out.println(new JSONObject(wangxiaoer).toString());

最终的运行效果同上

三.json数据的解析

此时会用到apache项目组下的commons-io jar包,maven依赖如下:

        <dependency>            <groupId>commons-io</groupId>            <artifactId>commons-io</artifactId>            <version>2.0</version>        </dependency>

代码如下:
DiaoSi javaBean:

package bean;import java.io.Serializable;public class DiaoSi implements Serializable {    /**     *      */    private static final long serialVersionUID = 1L;    private String name;    private double age;    private String birthday;    private String school;    private boolean has_girlfriend;    private Object car;    private Object house;    private String[] major;    private String comment;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getAge() {        return age;    }    public void setAge(double age) {        this.age = age;    }    public String getBirthday() {        return birthday;    }    public void setBirthday(String birthday) {        this.birthday = birthday;    }    public String getSchool() {        return school;    }    public void setSchool(String school) {        this.school = school;    }    public boolean isHas_girlfriend() {        return has_girlfriend;    }    public void setHas_girlfriend(boolean has_girlfriend) {        this.has_girlfriend = has_girlfriend;    }    public Object getCar() {        return car;    }    public void setCar(Object car) {        this.car = car;    }    public Object getHouse() {        return house;    }    public void setHouse(Object house) {        this.house = house;    }    public String[] getMajor() {        return major;    }    public void setMajor(String[] major) {        this.major = major;    }    public String getComment() {        return comment;    }    public void setComment(String comment) {        this.comment = comment;    }}

wangxiaoer.json json数据文件:

{    "name":"王小二",    "age":25.2,    "birthday":"1990-01-01",    "school":"蓝翔",    "major":["理发","挖掘机"],    "has_girlfriend":false,    "house":null,    "car":null,    "comment":"这是一个注释"}

从json数据文件中读取内容并解析:

// 返回的是当前Class这个类所在包开始的为置        File file = new File(ReadJsonSample.class.getResource("wangxiaoer.json").getFile());        String content = FileUtils.readFileToString(file);        JSONObject jsonObject = new JSONObject(content);        if(!jsonObject.isNull("name")){            System.out.println("姓名是:" + jsonObject.getString("name"));        }        if(!jsonObject.isNull("age")){            System.out.println("年龄是:" + jsonObject.getDouble("age"));        }        if(!jsonObject.isNull("major")){            JSONArray majorArray = jsonObject.getJSONArray("major");            for (Object object : majorArray) {                System.out.println(object.toString());            }        }

解析结果如下所示:
这里写图片描述

1 0