如何使用 Gson 解析深层嵌套 json 对象中的字段并在 android 中进行改造? [英] How do I parse a field from a deep nested json object using Gson and retrofit in android?

查看:117
本文介绍了如何使用 Gson 解析深层嵌套 json 对象中的字段并在 android 中进行改造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独特的情况,我必须从 json 的深层嵌套对象中获取某些时间.这有点复杂,我找不到解决方案,因此正在寻找解决此问题的想法和方法

I have a unique situation where I have to get certain times from a json's deeply nested object. It's a little complex, I couldn't find a solution so looking for ideas and ways to tackle this

我有一个如下的json:

I have a the json as follows:

[{
        "mySpaceId": 73220,
        "myBuildingId": 14019,
        "myFloorId": 10569,
        "myFloorNumber": "4",
        "myFloorName": "4th Floor",
        "spaceName": "My Room 4",
        "capacity": 5,
        "type": "huddle",
        "busyAt": []
    },
    {
        "mySpaceId": 73219,
        "myBuildingId": 14019,
        "myFloorId": 10569,
        "myFloorNumber": "4",
        "myFloorName": "4th Floor",
        "spaceName": "My room 5",
        "description": null,
        "capacity": 4,
        "type": "huddle",
        "timeZone": "America/New_York",

        "busyAt": [{
            "from": "2019-06-07T23:00:00+0000",
            "to": "2019-06-07T23:15:00+0000",
            "events": [{
                "id": "109142028",
                "series_id": null,
                "recurrence_id": null,
                "uid": "ABCDE",
                "space_id": 73219,
                "start": {
                    "date_time": "2019-06-07T19:00:00-0400",
                    "time_zone": "America/New_York"
                },
                "end": {
                    "date_time": "2019-06-07T19:15:00-0400",
                    "time_zone": "America/New_York"
                },
                "started_at": "2019-06-07T19:00:00-0400",
                "ended_at": "2019-06-07T19:15:00-0400"
            }]
        }]
    }
]

我使用这个:http://www.jsonschema2pojo.org/ 从在 json 字符串之上.我想知道如何检索started_at":2019-06-07T19:00:00-0400",

I use this : http://www.jsonschema2pojo.org/ to generate a class from the above json string. I was wondering how do I retrieve the "started_at": "2019-06-07T19:00:00-0400",

从busyAt-> 事件到由上述站点生成的我的主模型类?说与 mySpaceId 处于同一级别.我目前使用以下内容:

from busyAt-> events into my main model class generated by the above site? Say at the same level as mySpaceId. I currently use the following :

 public List<BusyAt> getBusyAt() {
        return busyAt;
    }

    public void setBusyAt(List<BusyAt> busyAt) {
        this.busyAt = busyAt;
    }

有没有办法在这个级别检索started_at并解析格式为:8:00 am 的日期和时间?在我的代码中使用它.

Is there a way I can retrieve the started_at at this level and parse the date and time in the format : 8:00 am ? to use it in my code.

知道怎么做吗?谢谢!如果这令人困惑或需要更多说明,请告诉我,很高兴发布更多代码.

Any idea how to go about this? Thanks! Please let me know if this is confusing or need more clarification, happy to post more code.

推荐答案

我使用这个:http://www.jsonschema2pojo.org/ 从上面的json字符串.我想知道如何取回"started_at": "2019-06-07T19:00:00-0400",从 busyAt-> 事件到我上面生成的主模型类地点?说与 mySpaceId 处于同一级别.我目前使用以下:

I use this : http://www.jsonschema2pojo.org/ to generate a class from the above json string. I was wondering how do I retrieve the "started_at": "2019-06-07T19:00:00-0400", from busyAt-> events into my main model class generated by the above site? Say at the same level as mySpaceId. I currently use the following :

如果我对您的理解正确,您已经使用 www.jsonschema2pojo.org 创建了以下类:-

If I interpret you correctly, you have created using the www.jsonschema2pojo.org the following classes: -

  • 一个名为Entity"的类,其中包含mySpaceId"和BusyAt"列表.
  • BusyAt"类包含事件"列表.
  • 事件"类包含一个名为 StartedAt 的字符串.

我假设您想直接从最顶层的类(实体")中检索每个列表的第一个条目(如果存在)

I assume that you want to retrieve the First entry of each list (if it exist) directly from the top-most class ("Entity")

类似:-

entity.busyAt(0).events(0).startedAt
如果busyAt 或事件列表为空或null,则为startedAt 返回空字符串.

entity.busyAt(0).events(0).startedAt
if either busyAt or event list is empty or null, then return empty string for startedAt.

您可以做的是在Entity"类(包含 mySpaceId 和 List 的根类)中创建以下方法.

what you can do, is to create the following method in the "Entity" class (root class containing both mySpaceId, and 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(); 
}

这篇关于如何使用 Gson 解析深层嵌套 json 对象中的字段并在 android 中进行改造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆