对象的解析
例如
{
"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": "惊心动魄"