Gson 和Json解析

{"result":1,"data":{"list":[{"type":1,"name":"\u65e9\u5b89\u6545\u4e8b","enname":"Mor.","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151101\/4f918044e69285a4ea45728ccbcb7c34.JPG!300300"},{"type":27,"name":"\u665a\u5b89\u6545\u4e8b","enname":"Nig.","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151101\/16609019f975e99fdb2c27eda52672a3.JPG!300300"},{"type":10,"name":"\u8bfb\u4e66","enname":"Book","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151030\/d895808e4f82a67301cfe6aa4714bdd0.JPG!300300"},{"type":14,"name":"\u4e07\u76f8","enname":"World","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151101\/2474f399b4a8d09e657d165f339d6529.JPG!300300"},{"type":6,"name":"\u5f71\u89c6","enname":"Movie","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151101\/3a8eab79711d0a40499fdcb5e6396259.JPG!300300"},{"type":18,"name":"\u5fc3\u7406","enname":"Psychology","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151029\/86e1e2b8390fa12de9259f8eb55f7682.JPG!300300"},{"type":12,"name":"\u8bbf\u8c08","enname":"Interview","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151026\/99d8048c1158fc9a17f10caf57968db5.JPG!300300"},{"type":11,"name":"\u4eba\u7269","enname":"People","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151029\/d263973e0d22f71e7cca74e27aa3ad3c.JPG!300300"},{"type":7,"name":"\u65c5\u884c","enname":"Travel","coverimg":"http:\/\/pkimg.image.alimmdn.com\/upload\/20151028\/e10b6d5cacfcbf3e10626283f0491404.JPG!300300"}],"carousel":[{"img":"http:\/\/pkicdn.image.alimmdn.com\/timeline\/tagimgs\/d7fdeee10be96fb99792a9cbfcae8f7d.jpg","url":"pianke:\/\/article\/55ebd2765d77431b448b4690"},{"img":"http:\/\/pkicdn.image.alimmdn.com\/timeline\/tagimgs\/a88e45ee104ed28b69cbb976df7a9cbd.jpg","url":"pianke:\/\/article\/562de24e5d7743921f8b4604"},{"img":"http:\/\/pkicdn.image.alimmdn.com\/timeline\/tagimgs\/f1c0e115511e4e8d644e967bdc18bb5b.png","url":"pianke:\/\/article\/55f246155e77430f7b8b4584"}]}}

用gson解析json时只需要解析json里的数组就行

public class Book {

    private  String type;
    private String name;
    private String enname;
    private String coverimg;

    @Override
    public String toString() {
        return "Book{" +
                "type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", enname='" + enname + '\'' +
                ", coverimg='" + coverimg + '\'' +
                '}';
    }

    public String getType() {
        return type;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEnname() {
        return enname;
    }

    public void setEnname(String enname) {
        this.enname = enname;
    }

    public String getCoverimg() {
        return coverimg;
    }

    public void setCoverimg(String coverimg) {
        this.coverimg = coverimg;
    }
}
 try {
                    JSONObject obj=new JSONObject(response);
                    JSONObject data=obj.getJSONObject("data");
                    JSONArray list=  data.getJSONArray("list");
                    for (int i=0;i<list.length();i++){
                        JSONObject item=list.getJSONObject(i);
                        Gson gson=new Gson();
                        Book book=gson.fromJson(item.toString(),Book.class);
                        Log.d("TAG", book.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

你可能感兴趣的:(Gson 和Json解析)