【问题标题】:How to parse such JSON with Gson?如何用 Gson 解析这样的 JSON?
【发布时间】:2019-04-11 03:42:40
【问题描述】:

我有一个具有这种结构的 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”这样的对象。他们的名字事先是未知的。 我可以解析“成功”字段和“错误”对象,但我遇到了“响应”对象的问题。我收到带有空响应对象的“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
}
}

【问题讨论】:

    标签: android json parsing gson retrofit


    【解决方案1】:

    json 字符串响应的模型,并使用 APIResponse 类使用 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;
    
    }
    

    【讨论】:

    • 这是一个有点奇怪的解决方案,不是吗?我总是得到不同的对象(“2018-11-19”、“2018-11-16”)。他们的名字取决于当前日期和其他不同的东西。这就是为什么我不能按名称映射这些对象
    【解决方案2】:

    对于您的固定/静态键模型,例如创建 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.
    

    【讨论】:

      【解决方案3】:

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

      public class AppointmentListApiResponse extends ApiResponse {
      
           @SerializedName("response")
           private Map<String, List<AppointmentInfo>> response;
           @SerializedName("success")
           private Boolean success;
           @SerializedName("error")
           private Error error;
      }
      

      而且它内部不需要 Response 类。希望这会对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2022-12-01
        • 2019-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多