JSON を GSON で解析する際に問題があります。
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 です。