我在使用 GSON 解析 JSON 时遇到问题。
JSON:
{ "data" : [ { "agent" : "Something",
"browser" : "app_j2me",
"campaign" : [ { "banner_type" : "mweb",
"campaign_type" : "cpc",
"cid" : "3",
"contentid" : "49fee4",
"ecpm" : "0.0804"
},
{ "banner_type" : "mweb",
"campaign_type" : "cpc",
"cid" : "22",
"contentid" : "1234",
"ecpm" : "0.1"
}
],
"cntr" : "th",
"imgsize" : "216x36",
"ip" : "123.123.123.123",
"partnerid" : 8959,
"url_click" : "...",
"url_img" : "...",
"url_show" : "...",
"version" : "2"
} ],
"error" : null
}
Java部分:
Gson gson = new Gson();
Json j = gson.fromJson(res, Json.class);
System.out.println(j);
课程:
class Json {
public Data d;
public Error r;
public String toString() {
return "Json [url_click=" + d.url_click
+ ", url_img=" + d.url_img
+ ", url_show=" + d.url_show
+ ", imgsize=" + d.imgsize
+ ", cntr=" + d.cntr
+ ", ip=" + d.ip
+ ", version=" + d.version + "]";
}
}
class Data {
public static String url_click;
public String url_img;
public String url_show;
public String imgsize;
public String cntr;
public String ip;
public String version;
public String browser;
public String agent;
public List<Campaign> campaign;
public String toString() {
return "Data [url_click=" + url_click
+ ", url_img=" + url_img
+ ", url_show=" + url_show
+ ", imgsize=" + imgsize
+ ", cntr=" + cntr
+ ", ip=" + ip
+ ", version=" + version + "]";
}
}
class Campaign {
public String banner_type;
public String campaign_type;
public String cid;
public String contentid;
public String ecpm;
}
class Error {
public String error;
}
运行此代码的结果:
Json [url_click=null, url_img=null, url_show=null, imgsize=null, cntr=null, ip=null, version=null]
我想这是类的问题,但现在知道如何让它正常工作。GSON 的版本为 2.2.2,Java 1.6。