如何使用Gson解析此类JSON?

时间:2018-11-07 16:46:46

标签: android json parsing gson retrofit

我有一个具有以下结构的JSON:

{
"response": {
    "2018-11-19": [
        {
            "date_end": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "10:30",
                "iso": "2018-11-19T10:30:00",
                "day": "19"
            },
            "date_start": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "08:00",
                "iso": "2018-11-19T08:00:00",
                "day": "19"
            },
            "id": "20181119080000001018"
        },
        {
            "date_end": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "10:30",
                "iso": "2018-11-19T10:30:00",
                "day": "19"
            },
            "date_start": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "09:30",
                "iso": "2018-11-19T09:30:00",
                "day": "19"
            },
            "id": "20181119093000001018"
        },
        {
            "date_end": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "10:30",
                "iso": "2018-11-19T10:30:00",
                "day": "19"
            },
            "date_start": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "10:00",
                "iso": "2018-11-19T10:00:00",
                "day": "19"
            },
            "id": "20181119100000001018"
        },
        {
            "date_end": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "10:30",
                "iso": "2018-11-19T10:30:00",
                "day": "19"
            },
            "date_start": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "10:30",
                "iso": "2018-11-19T10:30:00",
                "day": "19"
            },
            "id": "20181119103000001018"
        }
    ],
    "2018-11-16": [
        {
            "date_end": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "11:15",
                "iso": "2018-11-16T11:15:00",
                "day": "16"
            },
            "date_start": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "10:00",
                "iso": "2018-11-16T10:00:00",
                "day": "16"
            },
            "id": "20181116100000001018"
        },
        {
            "date_end": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "11:15",
                "iso": "2018-11-16T11:15:00",
                "day": "16"
            },
            "date_start": {
                "year": "2018",
                "day_verbose": "Sun",
                "month": "11",
                "month_verbose": "Nov",
                "time": "11:15",
                "iso": "2018-11-16T11:15:00",
                "day": "16"
            },
            "id": "20181116111500001018"
        }
    ]
},
"success": true,
"error": {}
}

可能存在许多类似“ 2018-11-19”的对象的主要困难。他们的名字事先是未知的。 我可以解析“成功”字段和“错误”对象,但使用“响应”对象时遇到麻烦。我收到带有空Response对象的“ OK” HTTP代码。我认为映射有问题。 这是我的映射类:

public class AppointmentListApiResponse extends ApiResponse {

@SerializedName("response")
@Expose
private Response response;
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("error")
@Expose
private Error error;

// getters and setters

public class Response {

    // I've tried these versions

    //private Map<String, AppointmentInfo> appoints;
    //private Map<String, List<AppointmentInfo>> appoints;
    //private List<List<AppointmentInfo>> appoints;

    // but they don't work for me

    // getters and setters
}

这是AppointmentInfo类:

public class AppointmentInfo {
@SerializedName("date_start")
@Expose
private Date dateStart;

@SerializedName("date_end")
@Expose
private Date dateEnd;

@SerializedName("id")
@Expose
private String id;

// getters and setters


public class Date {
    @SerializedName("day")
    @Expose
    private String day;

    @SerializedName("day_verbose")
    @Expose
    private String dayVerbose;

    @SerializedName("iso")
    @Expose
    private String dateTime;

    @SerializedName("month")
    @Expose
    private String month;

    @SerializedName("month_verbose")
    @Expose
    private String monthVerbose;

    @SerializedName("time")
    @Expose
    private String time;

    @SerializedName("year")
    @Expose
    private String year;

    // getters and setters
}
}

3 个答案:

答案 0 :(得分:0)

json字符串响应模型,并使用API​​Response类使用gson库解析json字符串

public class APIResponse{

@SerializedName("response")
@Expose
public Response response;
@SerializedName("success")
@Expose
public Boolean success;
@SerializedName("error")
@Expose
public Error error;

}

public class Response {

@SerializedName("2018-11-19")
@Expose
public List<com.example._20181119> _20181119 = null;
@SerializedName("2018-11-16")
@Expose
public List<com.example._20181116> _20181116 = null;

}

public class DateEnd {

@SerializedName("year")
@Expose
public String year;
@SerializedName("day_verbose")
@Expose
public String dayVerbose;
@SerializedName("month")
@Expose
public String month;
@SerializedName("month_verbose")
@Expose
public String monthVerbose;
@SerializedName("time")
@Expose
public String time;
@SerializedName("iso")
@Expose
public String iso;
@SerializedName("day")
@Expose
public String day;

}

public class DateEnd_ {

@SerializedName("year")
@Expose
public String year;
@SerializedName("day_verbose")
@Expose
public String dayVerbose;
@SerializedName("month")
@Expose
public String month;
@SerializedName("month_verbose")
@Expose
public String monthVerbose;
@SerializedName("time")
@Expose
public String time;
@SerializedName("iso")
@Expose
public String iso;
@SerializedName("day")
@Expose
public String day;

}

public class DateStart {

@SerializedName("year")
@Expose
public String year;
@SerializedName("day_verbose")
@Expose
public String dayVerbose;
@SerializedName("month")
@Expose
public String month;
@SerializedName("month_verbose")
@Expose
public String monthVerbose;
@SerializedName("time")
@Expose
public String time;
@SerializedName("iso")
@Expose
public String iso;
@SerializedName("day")
@Expose
public String day;

}

public class DateStart_ {

@SerializedName("year")
@Expose
public String year;
@SerializedName("day_verbose")
@Expose
public String dayVerbose;
@SerializedName("month")
@Expose
public String month;
@SerializedName("month_verbose")
@Expose
public String monthVerbose;
@SerializedName("time")
@Expose
public String time;
@SerializedName("iso")
@Expose
public String iso;
@SerializedName("day")
@Expose
public String day;

}

public class Error {


}



public class _20181116 {

@SerializedName("date_end")
@Expose
public DateEnd_ dateEnd;
@SerializedName("date_start")
@Expose
public DateStart_ dateStart;
@SerializedName("id")
@Expose
public String id;

}

public class _20181119 {

@SerializedName("date_end")
@Expose
public DateEnd dateEnd;
@SerializedName("date_start")
@Expose
public DateStart dateStart;
@SerializedName("id")
@Expose
public String id;

}

答案 1 :(得分:0)

对于您的固定/静态键模型,例如创建POJO-

"date_end": { "year": "2018", "day_verbose": "Sun", "month": "11", "month_verbose": "Nov", "time": "10:30", "iso": "2018-11-19T10:30:00", "day": "19" }

可以通过

进行映射
public class Date_End {
@SerializedName("year")
public String year;
@SerializedName("day_verbose")
public String dayVerbose;
@SerializedName("month")
public String month;
@SerializedName("monthVerbose")
public String monthVerbose;
@SerializedName("time")
public String time;
// and similarly other fields
}

为Date_END和Date_Start创建包装POJO

public class DateStartEndWrapper {
@SerializedName("date_end")
public Date_End dateEnd;
@SerializedName("date_start")
public Date_Start dateStart;
}

对于外部动态键(例如“ 2018-11-19”),应将响应视为Map并使用-解析它

Gson gson = new Gson();
Type mapType = new TypeToken<Map<String,DateStartEndWrapper>() {}.getType();
gson.fromJson() // pass the value of response key in here.

答案 2 :(得分:0)

我终于找到了解决方案。该类应该是这样的:

public class AppointmentListApiResponse extends ApiResponse {

     @SerializedName("response")
     private Map<String, List<AppointmentInfo>> response;
     @SerializedName("success")
     private Boolean success;
     @SerializedName("error")
     private Error error;
}

并且它里面不需要Response类。希望这会帮助某人。