2 ty0 0yt Ty0_0yT 于 2015.06.25 13:33 提问

用gson来解析带有[]的json数据

{
"res": null,
"time": "2015-06-25 09:23:45",
"data": {
"root": [
{
"name": "Supra 男跳舞高帮滑板鞋子shoes men vaider skytop k044h01",
"id": 10000000,
"lang": 0,
"brandId": 1,
"url": null,
"supplyName": "o1men",
"supplyId": 1,
"brandName": "canudilo h holidays",
"price": null
},
{
"name": "Supra 男跳舞高帮滑板鞋子shoes men vaider skytop v011黑色h01",
"id": 10000001,
"lang": 0,
"brandId": 1,
"url": null,
"supplyName": "o1men",
"supplyId": 1,
"brandName": "canudilo h holidays",
"price": null
},
{
"name": "Puma x Alife Men R698 Trinomic 357736-01银色 美版 新款 正品",
"id": 10000002,
"lang": 0,
"brandId": 1,
"url": null,
"supplyName": "o1men",
"supplyId": 1,
"brandName": "canudilo h holidays",
"price": null
}
],
"totalProperty": 21
},
"type": "object",
"success": true,
"lang": null
}

7个回答

qq_29271769
qq_29271769   2015.06.25 13:41

这个感觉看上去好复杂呀,我看有点难度,你有隐形云的账号吗?我们有个老技术员,专门是针对这块的,可以让他分享给你。

Ty0_0yT
Ty0_0yT 没有隐形云的账号,可不可让他把案例发到我的jianboy0_0@163.com这个邮箱呢,非常感谢
2 年多之前 回复
bdmh
bdmh   Ds   Rxr 2015.06.25 14:16

你需要定义一个和这个json结构对应的类,然后用gson直接转为类实例对象,如果你不想这样,就直接用JSONObject转成json对象b

Ty0_0yT
Ty0_0yT 怎么搞,能否分享一个案例呢,谢啦,我都搞了几天了,就没搞出来,
2 年多之前 回复
liuchunming2627
liuchunming2627   2015.06.25 14:30

首先简历一个GsonBean
public class GsonBean {
private String res;
private String time;
private DataBean data;
private String type;
private int lang;
private Boolean success;

public String getRes() {
    return res;
}

public void setRes(String res) {
    this.res = res;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public DataBean getData() {
    return data;
}

public void setData(DataBean data) {
    this.data = data;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public int getLang() {
    return lang;
}

public void setLang(int lang) {
    this.lang = lang;
}

public Boolean getSuccess() {
    return success;
}

public void setSuccess(Boolean success) {
    this.success = success;
}

}

然后简历一个DataBean
public class DataBean {
private List root;
private String totalProperty;

public List<RootBean> getRoot() {
    return root;
}

public void setRoot(List<RootBean> root) {
    this.root = root;
}

public String getTotalProperty() {
    return totalProperty;
}

public void setTotalProperty(String totalProperty) {
    this.totalProperty = totalProperty;
}

}

接着建立一个RootBean
public class RootBean {
private String name;
private int id;
private int lang;
private int brandId;
private String url;
private String supplyName;
private int supplyId;
private String brandName;
private String price;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getLang() {
    return lang;
}

public void setLang(int lang) {
    this.lang = lang;
}

public int getBrandId() {
    return brandId;
}

public void setBrandId(int brandId) {
    this.brandId = brandId;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getSupplyName() {
    return supplyName;
}

public void setSupplyName(String supplyName) {
    this.supplyName = supplyName;
}

public int getSupplyId() {
    return supplyId;
}

public void setSupplyId(int supplyId) {
    this.supplyId = supplyId;
}

public String getBrandName() {
    return brandName;
}

public void setBrandName(String brandName) {
    this.brandName = brandName;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

}

然后
GsonBean gsonBean = gson.fromJson("服务器返回的str", GsonBean.class);
这样你就可以拿到这个bean,以及这个bean里面封装的其他bean,也就是返回数据中的层级关系数据,get就可以了。如有不明白的再问我。

liuchunming2627
liuchunming2627   2015.06.25 14:45

DataBean
public class DataBean {
private List root;

liuchunming2627
liuchunming2627   2015.06.25 14:48

那个List里面包装一个rootBean 我这面复制上去之后就显示List root 你看get set方法后就知道怎么改了

Ty0_0yT
Ty0_0yT 按照你的方法写了,后面要下标,不知道怎么搞了
2 年多之前 回复
Ty0_0yT
Ty0_0yT http://img.ask.csdn.net/upload/201506/25/1435214873_631692.png
2 年多之前 回复
Ty0_0yT
Ty0_0yT ![图片说明](http://img.ask.csdn.net/upload/201506/25/1435215005_931152.png)
2 年多之前 回复
Ty0_0yT
Ty0_0yT   2015.06.25 14:48

图片说明按照你的方法做了,这里会报错,不知道怎么写了

liuchunming2627
liuchunming2627 这里面的arg0是索引 getRoot得到的是一个数组,argo = int 类型的
2 年多之前 回复
Vampire_Niklaus
Vampire_Niklaus   2015.06.25 15:12

我觉得还是 json转为map 比较好用一点,

Csdn user default icon
上传中...
上传图片
插入图片