简体   繁体   English

将Json映射到POJO

[英]Mapping Json to POJO

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

I am getting a response of this structure, how do i map it to my POJO class. 我得到了这种结构的响应,如何将其映射到我的POJO类。 Can anyone give a sample POJO class for this json structure. 任何人都可以为此json结构提供示例POJO类。 I am using Gson for Request and Response. 我正在使用Gson进行请求和响应。

Just map one Json Object as a Java Class. 只需将一个Json对象映射为Java类即可。 And make an Array of Object as a List.. 并制作一个对象数组作为列表。

Like, (pseudo code only) 喜欢,(仅限伪代码)

You are using GSON library so import SerializedName in your pojo classes. 您正在使用GSON库,因此在pojo类中导入SerializedName

import com.google.gson.annotations.SerializedName;

Pojo Class looks like, 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;
}

And main ListFileter class, 还有主要的ListFileter类,

public class ListFilter {

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

    }

And in Android code, 在Android代码中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM