Android的GSON:从相同的响应解析几个不同的对象 [英] Android GSON: Parsing several different objects from the same response

查看:119
本文介绍了Android的GSON:从相同的响应解析几个不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移植一个iPhone应用程序在很大程度上依赖于JSON到Android。
一个具有要解析响应的用于建立主屏幕。这样做的JSON响应包含3个不同的对象,即图标,标题和播放器。这些都包含在对象主页内。
图标和球员都含有项的Array,页眉只是一个单一的项目。

I'm working on porting an iPhone app that relies heavily on JSON to Android. One of the responses that has to be parsed is used to build the main screen. The JSON response for this contains 3 different objects, namely Icons, Header and Player. These are all contained within the object Home. Icons and Player both contain an Array of items, the Header is just a single item.

现在我还是个新手,当涉及到JSON,我不能肯定我应该如何解析此响应。因此,我想我的工作出现问题之前就知道如果我有个好主意。

Now I'm still a beginner when it comes to JSON, and I'm not quite sure how I should parse this response. Therefore I would like to know if I have the right idea before working myself into problems.

我的想法是创建4个不同的类,一个用于家庭,图标,页眉和播放器。
首页将同时包含图标和球员,和头的对象的数组。
但我不知道这是否是这样做的正确方法。

My idea is to create 4 different classes, one for Home, icons, Header and Player. Home would contain an array of both Icons and Player, and an object of Header. But I'm not sure if this is the correct way to do this.

在问题的JSON响应如下:(删除某些对象,由于反应的大小)

The JSON response in questions is as followed: (Removed some objects due to the size of the response)

{
  "Home": {
    "Icon": [
      {
        "ScreenID": 533,
        "ScreenIndex": 1,
        "IconName": "mainIcon_news",
        "Title": "News",
        "FK_ModuleID": 6,
        "FormID": 567,
        "ModName": "News",
        "MediaType": "",
        "New_Icon": 0
      },
      {
        "ScreenID": 528,
        "ScreenIndex": 2,
        "IconName": "mainIcon_music",
        "Title": "Music",
        "FK_ModuleID": 3,
        "FormID": 562,
        "ModName": "Media",
        "MediaType": "Music",
        "New_Icon": 0
      }
    ],
    "Header": [
      {
        "ModHomeRotationID": 183,
        "image_url": "*****/Media/68/1216_5.jpg",
        "flg_RotationEnabled": false,
        "flg_RotateOnlyOnReturn": true,
        "flg_RotationRandomize": false,
        "flg_RotationDelayMS": 5000,
        "flg_RotationDelayFadeMS": 3000,
        "HomeRotationIndex": null
      }
    ],
    "Player": [
      {
        "MediaID": 1219,
        "Track_Name": "***",
        "song_url": "*****/Media/68/1219.mp3",
        "song_remote_url": null,
        "FileSize": 4700502
      },
      {
        "MediaID": 1220,
        "Track_Name": "**** ",
        "song_url": "*****/Media/68/1220.mp3",
        "song_remote_url": null,
        "FileSize": 4350222
      }
    ]
  }
}

有人能告诉我,如果我在正确的方向是,如果没有,我应该怎样做呢?
我应该提到,我使用GSON解析此刻的JSON响应。

Could someone tell me if I'm in the right direction, and if not, what I should be doing instead? I should mention, I'm using GSON to parse the JSON responses at the moment.

在此先感谢

推荐答案

是你的权利,你需要创建四个类和需要初始化类名中的值。

Yes you are right you need to create Four classes and need to initialize the values inside that class name..

使用验证您的JSON JSONLint

Validate your Json using JSONLint :

那就试试这个样本<一个href=\"http://stackoverflow.com/questions/4253815/gson-no-args-constructor-for-class-xyz-with-arrays\">Parsing JSON使用GSON 和<一个href=\"http://stackoverflow.com/questions/6675714/phrasing-json-array-using-gson-for-google-search-api\">One详情

这篇关于Android的GSON:从相同的响应解析几个不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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