1.创建Gson:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
或者
Gson gson = new Gson();
2.json转对象
Object obj = gson.fromJson(jsonString, Object.class);
或者
Object obj = gson.fromJson(jsonString, new TypeToken
String str = "[
{
"hotword": "向艳梅斩获金牌",
"url": "http://m.yz2.sm.cn/s?q=%E5%90%91%E8%89%B3%E6%A2%85%E6%96%A9%E8%8E%B7%E9%87%91%E7%89%8C&by=hot&from=wm635479"
},
{
"hotword": "傅园慧网络直播",
"url": "http://m.yz2.sm.cn/s?q=%E5%82%85%E5%9B%AD%E6%85%A7%E7%BD%91%E7%BB%9C%E7%9B%B4%E6%92%AD&by=hot&from=wm635479"
},
{
"hotword": "亲哥偷走23万",
"url": "http://m.yz2.sm.cn/s?q=%E4%BA%B2%E5%93%A5%E5%81%B7%E8%B5%B023%E4%B8%87&by=hot&from=wm635479"
}
]";
public class Bean {
private String hotword;
private String url;
public String getHotword() {
return hotword;
}
public void setHotword(String hotword) {
this.hotword = hotword;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Bean bean = gson.fromJson(jsonString, new TypeToken<List<Bean>>(){}.getType());
或者
Bean bean = gson.fromJson(jsonString,Bean.class);