JSON解析(GSON/FastJSON)

常用的4中json

1、JSON官方 2、GSON GSON解析是谷歌的 3、FastJSON FastJSON是阿里巴巴的产品 4、jackson

1 []中括号代表的是一个数组;2 {}大括号代表的是一个对象 3 双引号“”表示的是属性值 4 冒号:代表的是前后之间的关系,冒号前面是属性的名称,后面是属性的值,这个值可以是基本数据类型,也可以是引用数据类型。

JSON值可以为以下几种

数字(整数或浮点数) 字符串(在双引号中) 逻辑值(true 或 false) 数组(在方括号中) 对象(在花括号中) null

可以镶套

举例:

{ "aab001":0 "aae397":null, "aae398":null, "aca111":"", "aca111Local":"", "aca111Rc":"", "aca118":"服务人员", "acb202":"", "acb207":null, "acb20c":"", "tPk":null, "compositeKeyStr":"", "visitcount":0 }

public class Book {
    private String id;
    private String  name;
    private String info;
​
    public Book() {
​
    }
    public Book(String id, String name, String info) {
        this.id = id;
        this.name = name;
        this.info = info;
    }
​
    public String getId() {
        return id;
    }
​
    public void setId(String id) {
        this.id = id;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public String getInfo() {
        return info;
    }
​
    public void setInfo(String info) {
        this.info = info;
    }
​
    @Override
    public String toString() {
        return "Book{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", info='" + info + '\'' +
                '}';
    }
}

GSON

public class Demo1 {
    public static void main(String[] args) {
        //创建GSON 对象
        Gson g = new Gson();
        //转换
        Book book = new Book("9999","孙悟空","筋斗云");
        String s = g.toJson(book);
        System.out.println(s);
​
​
        Book book1 = g.fromJson("{\"id\":\"9999\",\"name\":\"孙悟空\",\"info\":\"筋斗云\"}", Book.class);
        System.out.println(book1.getName());
    }
}

FastJSON

public class Demo4 {
    public static void main(String[] args) {
        Book book = new Book("9999","孙悟空","筋斗云");
        String s = JSON.toJSONString(book);
        System.out.println(s);
​
        Book book1 = JSON.parseObject("{\"id\":\"9999\",\"info\":\"筋斗云\",\"name\":\"孙悟空\"}", Book.class);
        System.out.println(book1.toString());
​
​
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值