Json解析

对象的解析

例如

{
"id": 1,
"name": "网游之灵魂守卫",
"imgPath": "http://192.168.31.43:8080/read/source_images/lt.jpg",
"author": "雷童的仰望",
"tag": "自传",
"desc": "写给曾经的一个女孩子,希望她能好好照顾自己"
}


分析

两个花括号 { } 包装起来的数据解析出来是一个对象,bean的成员属性要与json的一一对应


代码

public class Novel {
    private Integer id;
    private String name;
    private String imgPath;
    private String author;
    private String tag;
    private String desc;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getImgPath() {
        return imgPath;
    }
    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getTag() {
        return tag;
    }
    public void setTag(String tag) {
        this.tag = tag;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }

}
// json为要解析的数据,格式如上边的而例子
public Novel parseJson(String json) {
        Gson gson = new Gson();
        Novel novel = gson.fromJson(json, City.class);
        return novel ;
}

链表的解析(不包含链表名)

例子

[
    {
        "id": 1,
        "name": "网游之灵魂守卫",
        "imgPath": "http://192.168.31.43:8080/read/source_images/lt.jpg",
        "author": "雷童的仰望",
        "tag": "自传",
        "desc": "写给曾经的一个女孩子,希望她能好好照顾自己"
    },
    {
        "id": 26,
        "name": "三国演义",
        "imgPath": "http://192.168.31.43:8080/read/source_images/sanguo.jpg",
        "author": "罗贯中",
        "tag": "三国",
        "desc": "中国第一部长篇章回体历史演义小说,全名为《三国志通俗演义》"
    },
    {
        "id": 24,
        "name": "三个火枪手",
        "imgPath": "http://192.168.31.43:8080/read/source_images/san.jpg",
        "author": "大仲马",
        "tag": "惊心动魄"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值