为Complex Json对象创建一个GSON POJO类?

时间:2016-08-08 08:01:52

标签: android json gson retrofit jsonobject

我正在使用Gson GsonConverterFactory对我的API连接进行改造。我无法获取API。

{
    status: 100,
    tb_name: "list",
    data: {
        1: {
            distance: "",
            Busid: "18",
            locid: "6309",
            business_name: " Park",
            category_name: "Activities",
            area: "Sharjah",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Sharjah",
            lat_long: "25.342403, 55.379542",
            image: "14650439123004854.jpg",
            is_fav: "No"
        },
        2: {
            distance: "",
            Busid: "56",
            locid: "6311",
            business_name: "Bowling City",
            category_name: "Activities",
            area: "Abu Dhabi",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Abu Dhabi",
            lat_long: "24.494183, 54.367608",
            image: "14650440703886102.jpg",
            is_fav: "No"
        },
        3: {
            distance: "",
            Busid: "56",
            locid: "6312",
            business_name: "Bowling City",
            category_name: "Activities",
            area: "Deira, Dubai",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Dubai",
            lat_long: "25.251964, 55.332805",
            image: "14650440703886102.jpg",
            is_fav: "No"
        },
        4: {
            distance: "",
            Busid: "56",
            locid: "6310",
            business_name: "Bowling City",
            category_name: "Activities & Leisure",
            area: "Al Ain Mall, Al Ain",
            cat_image: "14645915394584643.png",
            cusine: "",
            city: "Al Ain",
            lat_long: "24.222321, 55.782205",
            image: "14650440703886102.jpg",
            is_fav: "No"
        }

    }
}

我创建了pojo类

public class ktbresponse {
    private String status;
    private String tb_name;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getTb_name() {
        return tb_name;
    }

    public void setTb_name(String tb_name) {
        this.tb_name = tb_name;
    }

    public application.myrestapi.com.myrestapiapplication.model.data getData() {
        return data;
    }

    public void setData(application.myrestapi.com.myrestapiapplication.model.data data) {
        this.data = data;
    }

    private data data;
}


public class data {
    private String distance;
    private String Busid;

    public String getDistance() {
        return distance;
    }

    public void setDistance(String distance) {
        this.distance = distance;
    }

    public String getBusid() {
        return Busid;
    }

    public void setBusid(String busid) {
        Busid = busid;
    }

    public String getLocid() {
        return locid;
    }

    public void setLocid(String locid) {
        this.locid = locid;
    }

    public String getBusiness_name() {
        return business_name;
    }

    public void setBusiness_name(String business_name) {
        this.business_name = business_name;
    }

    private String locid;
    private String business_name;
}

但是当我尝试打印时

Log.e("apiResponse",apiResponse.getStatus()); Log.e("apiResponse",apiResponse.getData().getBusiness_name());

仅打印status.not工作数据。我是第一次看到这种结构。让我知道我做错了什么。如何为这个结构创建Pojo类?

2 个答案:

答案 0 :(得分:2)

您是否可以修改API?如果是,只需将“data”对象更改为Array而不是具有字段“1”,“2”,“3”的对象。 Json看起来像:

{
status: 100,
tb_name: "list",
data: [
    {
        distance: "",
        Busid: "18",
        locid: "6309",
        business_name: " Park",
        category_name: "Activities",
        area: "Sharjah",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Sharjah",
        lat_long: "25.342403, 55.379542",
        image: "14650439123004854.jpg",
        is_fav: "No"
    },
    {
        distance: "",
        Busid: "56",
        locid: "6311",
        business_name: "Bowling City",
        category_name: "Activities",
        area: "Abu Dhabi",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Abu Dhabi",
        lat_long: "24.494183, 54.367608",
        image: "14650440703886102.jpg",
        is_fav: "No"
    },
    {
        distance: "",
        Busid: "56",
        locid: "6312",
        business_name: "Bowling City",
        category_name: "Activities",
        area: "Deira, Dubai",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Dubai",
        lat_long: "25.251964, 55.332805",
        image: "14650440703886102.jpg",
        is_fav: "No"
    },
    {
        distance: "",
        Busid: "56",
        locid: "6310",
        business_name: "Bowling City",
        category_name: "Activities & Leisure",
        area: "Al Ain Mall, Al Ain",
        cat_image: "14645915394584643.png",
        cusine: "",
        city: "Al Ain",
        lat_long: "24.222321, 55.782205",
        image: "14650440703886102.jpg",
        is_fav: "No"
    }    
  ]
}

在模型中你只有一个List

答案 1 :(得分:0)

Gson格式化程序可以解决您的问题。您可以从json模板创建模型。