简体   繁体   中英

complicated json to pojo

Can you please help me with designing pojo class for json like this, where property used as value?

{"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"}
    }
}

i want to get some like this

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

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

You can keep it as a Map of maps. and the inner time details as a custom object. So end result would be like Map<String, Map<String, Time>> . Then you a json parser like Gson or Jackson to parse the value. Eg

Jackson style

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 style

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());

You can try something like this:

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

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

Of course, you need to write a parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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