我用这个:http :
//www.jsonschema2pojo.org/从上面的json字符串生成一个类。我想知道如何从busyAt->事件中检索“
started_at”:“
2019-06-07T19:00:00-0400”到上述站点生成的主模型类中?说出与mySpaceId相同的级别。我目前使用以下内容:
如果我正确地理解了您,则说明您已经使用www.jsonschema2pojo.org创建了以下类别:-
- 一个名为“ Entity”的类,其中包含“ mySpaceId”和“ BusyAt”列表。
- 类“ BusyAt”包含“事件”列表。
- 类“事件”包含一个名为StartedAt的字符串。
我假设您想直接从最顶层的类(“实体”)检索每个列表的“第一”条目(如果存在)
就像是: -
如果busyAt或事件列表为空或为null,则entity.busyAt(0).events(0).startedAt
,然后为startedAt返回空字符串。
您可以做的是在“ Entity”类(包含mySpaceId和List的根类)中创建以下方法。
public String getStartedAt(){ //check if the busyAt List contains items or not. if (busyAt ==null || busyAt.isEmpty()){ return ""; } //take the list of events from the first busyAt in the array List<Event> eventList = busyAt.get(0).getEvents(); //check if the event List contains items or not. if (eventList ==null || eventList.isEmpty()){ return ""; } //return the StartAt value of the first event. return eventList.get(0).getStartedAt(); }


