首页 > 程序开发 > 软件开发 > Java > 正文
JSON快速入门(Java版)--慕课网笔记
2017-08-10 09:47:09       个评论    来源:学习笔记  
收藏    我要投稿
第1章 课程概述
1-1 JSON课程介绍

JSON是行业内使用最为广泛的数据传输格式

JSON基础知识 Java中两种常见的JSON处理方式 综合运用
第2章 基础入门
2-1 什么是JSON

JSON是一种与开发语言无关的、轻量级的数据格式。全称是JavaScript Object Notation。

优点:易于人的阅读和编写,易于程序解析与生成

{
    "name": "JSON快速入门(Java版)",
    "author": "李广",
    "content": [
        "JSON基础入门",
        "常用JSON处理"
    ],
    "time": {
        "value": 30,
        "unit": "分钟"
    }
}
2-2 数据类型表示
数据结构
Object
使用花括号包含键值对结构,Key必须是String类型,value为任何基本类型或数据结构 Array
使用中括号[]来起始,并用逗号,来分割元素。 基本类型
String number true false null
2-3 JSON数据演示
{
    "name": "王小二",
    "age": 25.2,
    "birthday": "1990-01-01",
    "school": "蓝翔",
    "major": [
        "理发",
        "挖掘机"
    ],
    "has_girlfriend": false,
    "car": null,
    "house": null,
    "comment": "这是一个注释"
}
第3章 JSON in Java
3-1 JSON使用
    
        org.json
        json
        20090211
    
public class JsonObjectSample {

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

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

    private static void jSONObjectSample() {
         JSONObject wangxiaoer = new JSONObject();
         Object nullObject = null;
         try {
            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("car", nullObject);
             wangxiaoer.put("house", nullObject);
             wangxiaoer.put("comment", "这是一个注释");
             System.out.println(wangxiaoer.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

}
3-2 使用Map 构建JSON
    private static void createJsonByMap() {
        Map wangxiaoer = new HashMap();
        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("car", nullObject);
        wangxiaoer.put("house", nullObject);
        wangxiaoer.put("comment", "这是一个注释");

        System.out.println(new JSONObject(wangxiaoer).toString());
    }
3-3 使用Java Bean构建对象
    private static void createJsonByBean() {
        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));
    }
public class Diaosi {

    private String name;
    private String school;
    private boolean has_girlfriend;
    private double age;
    private Object car;
    private Object house;
    private String[] major;
    private String comment;
    private String birthday;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    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;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }



}
3-4 从文件读取JSON
{
    "birthday": "1990-01-01",
    "major": [
        "理发",
        "挖掘机"
    ],
    "school": "蓝翔",
    "car": null,
    "name": "王小二",
    "has_girlfriend": false,
    "comment": "这是一个注释",
    "house": null,
    "age": 25.2
}
public class ReadJSONSample {


    public static void main(String[] args) throws IOException, JSONException {
        File file = new File(ReadJSONSample.class.getResource("wangxiaoer.json").getFile());
        String content = FileUtils.readFileToString(file);
        JSONObject jsonObject = new JSONObject(content);
        System.out.println("姓名是:"+jsonObject.getString("name"));
        System.out.println("年龄"+jsonObject.getDouble("age"));
        System.out.println("有没有女朋友?"+jsonObject.getBoolean("has_girlfriend"));
        JSONArray majorArray = jsonObject.getJSONArray("major");
        for(int i = 0;i < majorArray.length(); i++) {
            String m = (String)majorArray.get(i);
            System.out.println("专业-" + (i+1) + m);
        }
    }
}
3-5 从文件读取JSON 判断null
public class ReadJSONSample {


    public static void main(String[] args) throws IOException, JSONException {
        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("nickname")) {
            System.out.println("姓名是:"+jsonObject.getString("nickname"));
        }
        System.out.println("年龄"+jsonObject.getDouble("age"));
        System.out.println("有没有女朋友?"+jsonObject.getBoolean("has_girlfriend"));
        JSONArray majorArray = jsonObject.getJSONArray("major");
        for(int i = 0;i < majorArray.length(); i++) {
            String m = (String)majorArray.get(i);
            System.out.println("专业-" + (i+1) + m);
        }
    }
}
3-6 JSON 总结
Json中没有Date类型 通过JSON不能将JavaBean反解析回来
第4章 第四章 GSON的使用
4-1 GSON介绍

google-gson

4-2 GSON生成JSON数据
public class GsonCreateSample {

    public static void main(String[] args) {

        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("这是一个注释");

        Gson gson = new Gson();
        System.out.println(gson.toJson(wangxiaoer));
    }
}
public class Diaosi {

    @SerializedName("NAME")
    private String name;
    private String school;
    private boolean has_girlfriend;
    private double age;
    private Object car;
    private Object house;
    private String[] major;
    private String comment;
    private String birthday;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    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;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }



}
4-3 生成JSON数据
public class GsonCreateSample {

    public static void main(String[] args) {


        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("这是一个注释");
        wangxiaoer.setIgnore("不要看见我");

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy(){


            public String translateName(Field f) {
                if(f.getName().equals("name")) {
                    return "NAME";
                }
                return f.getName();
            }
        });
        Gson gson = gsonBuilder.create();
        System.out.println(gson.toJson(wangxiaoer));
    }
}
public class Diaosi {

    private String name;
    private String school;
    private boolean has_girlfriend;
    private double age;
    private Object car;
    private Object house;
    private String[] major;
    private String comment;
    private String birthday;
    private transient String ignore;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    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;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public String getIgnore() {
        return ignore;
    }
    public void setIgnore(String ignore) {
        this.ignore = ignore;
    }



}
4-4 GSON解析
public class GsonReadSample {

    public static void main(String[] args) throws IOException {
        File file = new File(ReadJSONSample.class.getResource("../jsonTest/wangxiaoer.json").getFile());
        String content = FileUtils.readFileToString(file);
        Gson gson = new Gson();
        Diaosi wangxiaoer = gson.fromJson(content, Diaosi.class);
        System.out.println(wangxiaoer.toString());
    }

}
public class Diaosi {

    private String name;
    private String school;
    private boolean has_girlfriend;
    private double age;
    private Object car;
    private Object house;
    private String[] major;
    private String comment;
    private String birthday;
    private transient String ignore;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    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;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public String getIgnore() {
        return ignore;
    }
    public void setIgnore(String ignore) {
        this.ignore = ignore;
    }
    @Override
    public String toString() {
        return "Diaosi [name=" + name + ", school=" + school + ", has_girlfriend=" + has_girlfriend + ", age=" + age
                + ", car=" + car + ", house=" + house + ", major=" + Arrays.toString(major) + ", comment=" + comment
                + ", birthday=" + birthday + "]";
    }



}
4-5 GSON解析 带日期转换
public class GsonReadSample {

    public static void main(String[] args) throws IOException {
        File file = new File(ReadJSONSample.class.getResource("../jsonTest/wangxiaoer.json").getFile());
        String content = FileUtils.readFileToString(file);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        DiaosiWithBirthday wangxiaoer = gson.fromJson(content, DiaosiWithBirthday.class);
        System.out.println(wangxiaoer.getBirthday().toLocaleString());
    }

}
public class DiaosiWithBirthday {

    private String name;
    private String school;
    private boolean has_girlfriend;
    private double age;
    private Object car;
    private Object house;
    private String[] major;
    private String comment;
    private Date birthday;
    private transient String ignore;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    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;
    }
    public String getIgnore() {
        return ignore;
    }
    public void setIgnore(String ignore) {
        this.ignore = ignore;
    }

    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "Diaosi [name=" + name + ", school=" + school + ", has_girlfriend=" + has_girlfriend + ", age=" + age
                + ", car=" + car + ", house=" + house + ", major=" + Arrays.toString(major) + ", comment=" + comment
                + ", birthday=" + birthday + "]";
    }



}
4-6 集合类解析
public class GsonReadSample {

    public static void main(String[] args) throws IOException {
        File file = new File(ReadJSONSample.class.getResource("../jsonTest/wangxiaoer.json").getFile());
        String content = FileUtils.readFileToString(file);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        DiaosiWithBirthday wangxiaoer = gson.fromJson(content, DiaosiWithBirthday.class);
        System.out.println(wangxiaoer.getBirthday().toLocaleString());

        System.out.println(wangxiaoer.getMajor());
        System.out.println(wangxiaoer.getMajor().getClass());
    }

}
public class DiaosiWithBirthday {

    private String name;
    private String school;
    private boolean has_girlfriend;
    private double age;
    private Object car;
    private Object house;
    private List major;
    private String comment;
    private Date birthday;
    private transient String ignore;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    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 getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
    public String getIgnore() {
        return ignore;
    }
    public void setIgnore(String ignore) {
        this.ignore = ignore;
    }

    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public List getMajor() {
        return major;
    }
    public void setMajor(List major) {
        this.major = major;
    }


}
4-7 总结
JSON时Android SDK官方的库 GSON适用于服务端开发
GSON比JSON功能更强大

关于JSON库的总结
功能:映射Java Object与json格式数据

通过Annotation注解来声明。 支持自定义属性名称。 支持包含或排除属性。 支持自定义接口完成解析/生成过程
点击复制链接 与好友分享!回本站首页
上一篇:java编程思想笔记-并发之
下一篇:Java反射机制--Class类
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做实用的IT技术学习网站