为了账号安全,请及时绑定邮箱和手机立即绑定

有关JSON以及JSONP的总结

标签:
JavaScript
什么是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);
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消