栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Gson解析深层嵌套json对象中的字段并在android中进行改造?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Gson解析深层嵌套json对象中的字段并在android中进行改造?

我用这个: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(); }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392467.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号