您的位置:首页 > Web前端 > JavaScript

使用Gson解析json数据

2016-07-27 00:47 471 查看
使用Gson解析json数据,非常方便,要细心的配置实体类Model。


比如json字符串为:

{
"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body": {
"allPages": 1503,
"ret_code": 0,
"contentlist": [
{
"id": "579758166e366c56d29064f5",
"text": "寒冬的早晨,你在池中奋力地划水,蛙泳、仰泳、蝶泳、自由泳,还有令人折服倾倒的潜泳!岸上的老汉急了:“你这坏小子!你喝干了粪池子,不让俺种地了!",
"title": "搞笑的恶&心的整人的短信",
"type": 1,
"ct": "2016-07-26 20:31:18.023"
},
{
"id": "579758166e366c56d29064f4",
"text": "我昨晚做了个梦,梦见太白金星告诉我很多天机!原来咱俩五百年前都是神仙。我哭了,我都想起来了!啸天,我是二郎啊!你还记得我么,啸天!这几百年还好吗?",
"title": "经典短信42个,逗乐你!",
"type": 1,
"ct": "2016-07-26 20:31:18.023"
}
],
"currentPage": 1,
"allNum": 30042,
"maxResult": 20
}
}```


照上面的json数据,Model实体类为:

public class joke {

private String showapi_res_code;
private String showapi_res_error;
private body showapi_res_body;
public static class body{
private String allPages;
private String ret_code;
private String currentPage;
private String allNum;
private String maxResult;
private List<jokemodel> contentlist;

public String getAllPages() {
return allPages;
}

public void setAllPages(String allPages) {
this.allPages = allPages;
}

public String getRet_code() {
return ret_code;
}

public void setRet_code(String ret_code) {
this.ret_code = ret_code;
}

public String getCurrentPage() {
return currentPage;
}

public void setCurrentPage(String currentPage) {
this.currentPage = currentPage;
}

public String getAllNum() {
return allNum;
}

public void setAllNum(String allNum) {
this.allNum = allNum;
}

public String getMaxResult() {
return maxResult;
}

public void setMaxResult(String maxResult) {
this.maxResult = maxResult;
}

public List<jokemodel> getContentlist() {
return contentlist;
}

public void setContentlist(List<jokemodel> contentlist) {
this.contentlist = contentlist;
}

public static  class jokemodel{
private String id;
private String text;
private String title;
private int type;
private String ct;

public String getId() {
return id;
}

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

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getType() {
return type;
}

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

public String getCt() {
return ct;
}

public void setCt(String ct) {
this.ct = ct;
}
}
}

public String getShowapi_res_code() {
return showapi_res_code;
}

public void setShowapi_res_code(String showapi_res_code) {
this.showapi_res_code = showapi_res_code;
}

public String getShowapi_res_error() {
return showapi_res_error;
}

public void setShowapi_res_error(String showapi_res_error) {
this.showapi_res_error = showapi_res_error;
}

public body getShowapi_res_body() {
return showapi_res_body;
}

public void setShowapi_res_body(body showapi_res_body) {
this.showapi_res_body = showapi_res_body;
}


}

注意实体类中的子类必须是static静态类修饰。字段名称必须与json中的键的名称一致。

在调用时使用下面的方法:


public void Gson(String json){

Gson gson = new Gson();

Type type = new TypeToken(){}.getType();

joke j = gson.fromJson(json,type);

for (int i = 0; i < j.getShowapi_res_body().getContentlist().size(); i++) {

Log.i(“setOnClickListener”,”Title:”+j.getShowapi_res_body().getContentlist().get(i).getTitle()+”,Content:”+j.getShowapi_res_body().getContentlist().get(i).getText());

}

}

“`

github源码地址如下:

https://github.com/fsdsds/GsonDemo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gson json 数据