复杂的json到pojo

时间:2014-08-07 09:09:04

标签: java android json jackson

你可以帮我设计像这样的json的pojo类,其中property用作值吗?

{"1":
    {
    "12.08.2014":
        {"start_time":"09:00","end_time":"11:00"},
    "15.08.2014":
        {"start_time":"09:00","end_time":"11:00"},
    }
,
"2":
    {
    "25.08.2014":
        {"start_time":"09:00","end_time":"11:00"},
    "27.08.2014":
        {"start_time":"09:00","end_time":"11:00"},
    "29.08.2014":
        {"start_time":"09:00","end_time":"11:00"}
    }
}

我希望得到一些这样的

class POJO {
    class Schedule{
        @JsonProperty("start_time")
        String startTime;
        @JsonProperty("end_time")
        String endTime;

        @JSON???????
        String date;
    }
    @?????????
    List<Schedule> scheduleList;
}

2 个答案:

答案 0 :(得分:2)

您可以将其保存为地图地图。并将内部时间细节作为自定义对象。所以最终结果就像Map<String, Map<String, Time>>。然后你使用像GsonJackson这样的json解析器来解析值。例如

杰克逊风格

public class Time {
    @JsonProperty(value="start_time")
    private String startTime;
    @JsonProperty(value="end_time")
    private String endTime;

    // Getter Setter
}

ObjectMapper mapper = new ObjectMapper();
Map<String, Map<String, Time>> data = mapper.readValue(json, new TypeReference<Map<String, Map<String, Time>>>() {});

Gson风格

class Time {
    @SerializedName(value="start_time")
    private String startTime;
    @SerializedName(value="end_time")
    private String endTime;

    // Getters Setters
}

Gson gson = new Gson();
Map<String, Map<String, Time>> data = gson.fromJson(json, new TypeToken<Map<String, Map<String, Time>>>() {}.getType());

答案 1 :(得分:0)

您可以尝试这样的事情:

class MyItem{
    String id;
    List<MyEvent> events;
}

class MyEvent{
    Date eventDate;
    int startTime;
    int endTime;
}

当然,你需要编写一个解析器。