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.
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.
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.
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
}
]
}
}
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.
Thanks in advance
Yes you are right you need to create Four classes and need to initialize the values inside that class name..
Validate your Json using JSONLint :
Then try this sample Parsing JSON using GSON and One More