使用Gson解析json数据

使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值