将Json映射到POJO

时间:2012-11-05 12:20:16

标签: java android gson

{
    "Filters": [{
        "Decription": "Default",
        "FieldSelected": {
            "AppointmentDate": true,
            "AppointmentDateOrder": 1            
            "ptStatusOrder": 3
        },
        "FilterID": 1
    }, {
        "Decription": "chart",
        "FieldSelected": {
            "AppointmentDate": true,
            "AppointmentDateOrder": 1,           
            "ptStatusOrder": 0
        },
        "FilterID": 2
    }]
}

我得到了这个结构的响应,我如何将它映射到我的POJO类。任何人都可以为此POJO结构提供示例json类。我正在使用Gson进行请求和响应。

1 个答案:

答案 0 :(得分:1)

只需将一个Json对象映射为Java类。并将对象数组作为列表..

喜欢,(仅限伪代码)

您正在使用 GSON 库,因此请在您的pojo类中导入SerializedName

import com.google.gson.annotations.SerializedName;

Pojo Class看起来像,

public class Filter {

    @SerializedName("Decription") // This requires same as your Json key 
    public String description;
    @SerializedName("FieldSelected") // The Json Object of FieldSelected
    public Field listDetails;

}

public class Field {
    @SerializedName("ptStatusOrder")
    public int status;
    @SerializedName("AppointmentDateOrder")
    public int dateOrder;
    @SerializedName("AppointmentDate")
    public boolean appDate;
}

主要 ListFileter 类,

public class ListFilter {

        @SerializedName("Filters") 
        public List<Filter> listFilter;

    }

在Android代码中,

Gson gson = new Gson();
ListFilter listFilter = gson.fromJson(jsonResponse, ListFilter.class);