安卓:使用GSON解析动态域响应 [英] Android: Using GSON to parse a response with dynamic fields

查看:204
本文介绍了安卓:使用GSON解析动态域响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析关于一个议程,其中一个领域是动态事件的JSON响应。该领域的变化取决于在该事件发生的月份。

I need to parse a JSON response regarding events in an agenda, of which a field is dynamic. This field changes depending on the month in which the event takes place.

现在我一直在GSON解析数据之前,但这些都有静态域所以它是很容易创造必要的POJO的。不过,我会怎么做这个动态领域?

Now I've been parsing data with GSON before, but these all had static fields so it was quite easy to create the necessary POJO's. But how would I do this with dynamic fields?

我需要解析的响应是,像这样:

The response i need to parse is like so:

{
  "Agenda/Future": {
    "November 2011/0": [
      {
        "id": "5675",
        "eventid": "",
        "name": "testing testing 123",
        "startdate": "nov 25",
        "datecompute": "2011-11-25T08:00:00",
        "group_month": "November 2011",
        "flg_Data": "Database"
      }
    ],
    "February 2012/1": [
      {
        "id": "5681",
        "eventid": "",
        "name": "dfd",
        "startdate": "feb 3",
        "datecompute": "2012-02-03T12:00:00",
        "group_month": "February 2012",
        "flg_Data": "Database"
      },
      {
        "id": "5679",
        "eventid": "",
        "name": "vfvd",
        "startdate": "feb 17",
        "datecompute": "2012-02-17T12:00:00",
        "group_month": "February 2012",
        "flg_Data": "Database"
      }
    ],
    "February 2013/2": [
      {
        "id": "5680",
        "eventid": "",
        "name": "df",
        "startdate": "feb 14",
        "datecompute": "2013-02-14T12:00:00",
        "group_month": "February 2013",
        "flg_Data": "Database"
      }
    ],
    "September 2013/3": [
      {
        "id": "5677",
        "eventid": "",
        "name": "fsdfsd",
        "startdate": "sep 14",
        "datecompute": "2013-09-14T12:00:00",
        "group_month": "September 2013",
        "flg_Data": "Database"
      }
    ],
    "November 2015/4": [
      {
        "id": "5676",
        "eventid": "",
        "name": "fsdfsd",
        "startdate": "nov 13",
        "datecompute": "2015-11-13T12:00:00",
        "group_month": "November 2015",
        "flg_Data": "Database"
      }
    ]
  }
}

正如你所看到的,关于个月目标标题是动态的。无论是在标题末尾的值,以及标题本身改变基于实际月和位置月阵列中

As you can see, the object title regarding months is dynamic. Both the value at the end of the title, as well as the title itself change based on the actual month and position in the array of months.

从我所看到的在这里,以便其他的问题,我需要用工作的地图
但我不能肯定我怎么会去这样做。
比方说,我创建了一个名为事件包含月份的阵列中的个别事​​件POJO,会是什么我初始化样子?

From what I've seen on other questions here at SO, I will need to work with Maps. But I'm not quite sure how I would go about doing so. Let's say I create a POJO called Event for the individual events contained in the array of the months, what would my initialization look like?

问候

我以杰克逊默认地图试图阿米尔的建议。
可悲的是,这将创建一个地图,大小 1 的。整个JSON响应被解析成一个对象。 Ofcourse,这不是我想要的,因为我需要从个别事件获取数据。

I have tried Amir's suggestion by using Jackson with the default Map. Sadly, this creates a Map with a size of 1. The entire JSON response gets parsed into a single object. Ofcourse, this is not what I want, since I need to get the data from the individual events.

有没有人有一个建议,我怎么可能是能够做到这一点?一般情况下我弄明白这些事情得很快,但我无法将围绕这一头,由于动态对象命名换行。

Does anyone have a suggestion as to how I might be able to do that? Normally I'd figure out these things quite quickly but I can't wrap my head around this one due to the dynamic object naming.

我最终设法破解这一难题。最后我用普通的一个JSONObjects,最终的工作。

I have eventually managed to crack this problem. I ended up using plain JSONObjects, which worked eventually.

在code我曾经得到它的工作如下:

The code I used to get it to work is as follows:

        JSONObject jsonObject = new JSONObject(response);
        JSONObject agenda = jsonObject.getJSONObject("Agenda/Future");

        if (agenda != null) {
            System.out.println(agenda.length());
            JSONArray events = agenda.names();
            if (events.length() > 0) {
                System.out.println("At least its bigger then 0. It's: "
                        + events.length());
                for (int i = 0; i < events.length(); i++) {
                    System.out.println(events.get(i).toString());
                    JSONArray test = agenda.getJSONArray(events.get(i)
                            .toString());
                    if (test != null) {
                        JSONObject testt = test.getJSONObject(0);
                        if (testt != null) {
                            System.out.println(testt.getString("name"));
                        } else {
                            System.out.println("Still empty, Check again.");
                        }
                    }
                }
            }

        } else {
            System.out.println("Agenda is completely empty, try again.");
        }

正如你所看到的,这还包含测试的名字,但至少它的作品。
感谢您的帮助大家!

As you can see, this still contains test-names, but at least it works. Thanks for the help everyone!

推荐答案

由于一个月的数据是无效的Java标识符,您将可能要使用自定义序列化 与GSON。你说的是使用地图对象,但如果你要做到这一点你不妨我们在 org.json 喜欢的JSONObject对象,这是基本建成在地图上的/哈希表反正。

Since the month data are not valid java identifiers, you 'll probably have to use custom serialization with GSON. You're talking about using Map objects but if you are going to do that you may as well us the org.json objects like JSONObject which are basically built on maps/hashtables anyway.

这篇关于安卓:使用GSON解析动态域响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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