继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

有关JSON以及JSONP的总结

怯金阁
关注TA
已关注
手记 4
粉丝 7
获赞 26
什么是JSON

与开发语言无关,轻量级的数据格式。

JSON采用key-value的形式形容数据,key必须为string类型,value可以为任何基本类型或数据结构。

{
    "name" : "怯金阁",
    "age" : 21,
    "birthday" : "1996-02-02",
    "school" : "哈尔滨学院",
    "major" : ["软件工程","计算机科学与技术"],
    "has_girlfriend" : false,
    "car" : null,
    "house" : null,
    "comment" : "This is a comment"
}

使用Java语言来处理JSON,有两种方法
1、使用JSONObject

JSONObject kaijinge = new JSONObject();
kaijinge.put("name", "怯金阁");
kaijinge.put("age", 21);
kaijinge.put("birthday", "1996-02-02");
kaijinge.put("school", "哈尔滨学院");
kaijinge.put("major", new String[]{"软件工程","计算机科学与技术"});
kaijinge.put("has_girlfriend", false);
kaijinge.put("car", null);
kaijinge.put("house", null);
kaijinge.put("comment", "这是一个注释");

System.out.println(kaijinge.toString());

2、使用Map

Map<String,Object> kaijinge = new HashMap<String,Object>();
kaijinge.put("name", "怯金阁");
kaijinge.put("age", 21);
kaijinge.put("birthday", "1996-02-02");
kaijinge.put("school", "哈尔滨学院");
kaijinge.put("major", new String[]{"软件工程","计算机科学与技术"});
kaijinge.put("has_girlfriend", false);
kaijinge.put("car", null);
kaijinge.put("house", null);
kaijinge.put("comment", "这是一个注释");

JSONObject jsonObject = JSONObject.fromObject(kaijinge);
System.out.println(jsonObject.toString());

3、使用JavaBean

public class kaijinge {
    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 hasGirlfriend) {
        has_girlfriend = hasGirlfriend;
    }
    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;
    }
}
kaijinge kaijinge = new kaijinge();
kaijinge.setName("怯金阁");
kaijinge.setAge(21);
kaijinge.setBirthday("1996-02-02");
kaijinge.setCar(null);
kaijinge.setComment("这是一条注释");
kaijinge.setHas_girlfriend(false);
kaijinge.setHouse(null);
kaijinge.setSchool("哈尔滨学院");
kaijinge.setMajor(new String[]{"软件工程","计算机科学与技术"});

JSONObject jsonObject = JSONObject.fromObject(kaijinge);
System.out.println(jsonObject.toString());

如何解析JSON数据
1、引入commons-io包
2、在项目里创建一个json文件,里面存入我们的json数据

File file = 
            new File(ReadJSONSample.class.getResource("/kaijinge.json").getFile());//声明文件
String content = FileUtils.readFileToString(file);//拿到数据
JSONObject jsonObject = JSONObject.fromObject(content);//转为json
System.out.println("姓名是 "+jsonObject.getString("name"));//读取json文件

JSONArray majorArray = jsonObject.getJSONArray("major");

for(int i=0;i<majorArray.size();i++){
    String string_majorArray = (String)majorArray.get(i);
    System.out.println(string_majorArray);
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP