这个问题是我早期的一个问题的后续问题,可以在这里找到:Android GSON: Parsing several different objects from the same response
我按照那里发布的答案,为我的JSON响应创建了4个单独的类。 但是,在解析此响应时,会生成对象(或者我认为,因为没有发生解析错误),但是当试图找出这些对象的数据时,会发生NullPointers。
我的JSON响应如下:
{
"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
}
]
}
}
我解析此响应的代码如下:
package com.mobowski.app.menusections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mobowski.app.json.WebService;
import com.mobowski.app.listitems.Item;
import com.google.gson.Gson;
import android.app.Activity;
import android.os.Bundle;
//import android.widget.Toast;
public class TestMain extends Activity {
ArrayList<Item> items = new ArrayList<Item>();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// setContentView(R.layout.mainlinks);
retrieveLinks();
}
public void retrieveLinks() {
// Instantiate the Web Service Class with he URL of the web service not
// that you must pass
WebService webService = new WebService(
"http://editedduetosecurityreasons");
// Pass the parameters
Map<String, String> params = new HashMap<String, String>();
params.put("iAppID", "59");
params.put("iUserID", "1");
params.put("strCulName", "");
// params.put("var", "");
// Get JSON response from server the "" are where the method name would
// normally go if needed example
// webService.webGet("getMoreAllerts", params);
String response = webService.webGet("", params);
System.out.println("Returns: " + response);
try {
Home collection = new Gson().fromJson(response, Home.class);
for (Icon icon : collection.icons) {
System.out.println(icon.title);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public class Home {
public List<Icon> icons;
public Header header;
public Player player[];
public List<Icon> getIcons() {
return icons;
}
public void setIcons(List<Icon> icons) {
this.icons = icons;
}
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
public Player[] getPlayer() {
return player;
}
public void setPlayer(Player[] player) {
this.player = player;
}
public Player getSinglePlayer(int i) {
return player[i];
}
public Icon getSingleIcon(int i) {
return icons.get(i);
}
}
public class Icon {
public int ScreenID;
public int ScreenIndex;
public String IconName;
public String title;
public int FK_ModuleID;
public int FormID;
public String ModName;
public String MediaType;
public int New_Icon;
public int getScreenID() {
return ScreenID;
}
public void setScreenID(int screenID) {
ScreenID = screenID;
}
public int getScreenIndex() {
return ScreenIndex;
}
public void setScreenIndex(int screenIndex) {
ScreenIndex = screenIndex;
}
public String getIconName() {
return IconName;
}
public void setIconName(String iconName) {
IconName = iconName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getFK_ModuleID() {
return FK_ModuleID;
}
public void setFK_ModuleID(int fK_ModuleID) {
FK_ModuleID = fK_ModuleID;
}
public int getFormID() {
return FormID;
}
public void setFormID(int formID) {
FormID = formID;
}
public String getModName() {
return ModName;
}
public void setModName(String modName) {
ModName = modName;
}
public String getMediaType() {
return MediaType;
}
public void setMediaType(String mediaType) {
MediaType = mediaType;
}
public int getNew_Icon() {
return New_Icon;
}
public void setNew_Icon(int new_Icon) {
New_Icon = new_Icon;
}
}
public class Player {
public int MediaID;
public String Track_Name;
public String song_url;
public String song_remote_url;
public int FileSize;
public int getMediaID() {
return MediaID;
}
public void setMediaID(int mediaID) {
MediaID = mediaID;
}
public String getTrack_Name() {
return Track_Name;
}
public void setTrack_Name(String track_Name) {
Track_Name = track_Name;
}
public String getSong_url() {
return song_url;
}
public void setSong_url(String song_url) {
this.song_url = song_url;
}
public String getSong_remote_url() {
return song_remote_url;
}
public void setSong_remote_url(String song_remote_url) {
this.song_remote_url = song_remote_url;
}
public int getFileSize() {
return FileSize;
}
public void setFileSize(int fileSize) {
FileSize = fileSize;
}
}
public class Header {
public int ModHomeRotationID;
public String image_url;
public boolean flg_RotationEnabled;
public boolean flg_RotateOnlyOnReturn;
public boolean flg_RotationRandomize;
public int flg_RorationDelayMS;
public int flg_RotationDelayFadeMS;
public int HomeRotationIndex;
public int getModHomeRotationID() {
return ModHomeRotationID;
}
public void setModHomeRotationID(int modHomeRotationID) {
ModHomeRotationID = modHomeRotationID;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public boolean isFlg_RotationEnabled() {
return flg_RotationEnabled;
}
public void setFlg_RotationEnabled(boolean flg_RotationEnabled) {
this.flg_RotationEnabled = flg_RotationEnabled;
}
public boolean isFlg_RotateOnlyOnReturn() {
return flg_RotateOnlyOnReturn;
}
public void setFlg_RotateOnlyOnReturn(boolean flg_RotateOnlyOnReturn) {
this.flg_RotateOnlyOnReturn = flg_RotateOnlyOnReturn;
}
public boolean isFlg_RotationRandomize() {
return flg_RotationRandomize;
}
public void setFlg_RotationRandomize(boolean flg_RotationRandomize) {
this.flg_RotationRandomize = flg_RotationRandomize;
}
public int getFlg_RorationDelayMS() {
return flg_RorationDelayMS;
}
public void setFlg_RorationDelayMS(int flg_RorationDelayMS) {
this.flg_RorationDelayMS = flg_RorationDelayMS;
}
public int getFlg_RotationDelayFadeMS() {
return flg_RotationDelayFadeMS;
}
public void setFlg_RotationDelayFadeMS(int flg_RotationDelayFadeMS) {
this.flg_RotationDelayFadeMS = flg_RotationDelayFadeMS;
}
public int getHomeRotationIndex() {
return HomeRotationIndex;
}
public void setHomeRotationIndex(int homeRotationIndex) {
HomeRotationIndex = homeRotationIndex;
}
}
}
但是这段代码似乎不起作用。调用for (Icon icon : collection.icons) {
时会出现nullpointer。当任何方法用于实际应该实现的任何项目时,会出现NullPointer,但Home对象除外。
我确信这意味着我做错了什么,但我不知道我做错了什么或在哪里。有人可以给我一些指示吗?
提前感谢任何愿意尝试的人
在尝试Venky的建议之后,现在数据似乎正在解析得更好。但是我仍然面临一个问题。在尝试解析Header对象时,LogCat向我显示以下错误:
com.google.gson.JsonParseException: Expecting object found: [{"ModHomeRotationID":162,"image_url":"***********/1020_5.jpg","flg_RotationEnabled":false,"flg_RotateOnlyOnReturn":true,"flg_RotationRandomize":false,"flg_RotationDelayMS":5000,"flg_RotationDelayFadeMS":3000,"HomeRotationIndex":null}]
我知道这意味着它希望收到一个对象,但却得到了一个数组。但是,我的代码是这样的:
public class Header {
List<Header_info> header;
}
public class Header_info {
public int ModHomeRotationID;
public String image_url;
public boolean flg_RotationEnabled;
public boolean flg_RotateOnlyOnReturn;
public boolean flg_RotationRandomize;
public int flg_RorationDelayMS;
public int flg_RotationDelayFadeMS;
public int HomeRotationIndex;
}
我对Icon对象使用了相同的方法,这些方法似乎解析得很好(没有错误)。 有什么建议为什么这不适用于Header对象?它似乎与图标格式相同。
答案 0 :(得分:1)
使用JSONLint
验证上述回复检查Logcat中的输出:由于使用Log命令打印输出:
相同类型的输出是Parsed,根据您的需要进行更改。
只需检查响应和群组对象
Java代码:
Gson gson = new Gson();
RecordResponse cashResponse = null;
try {
cashResponse = gson.fromJson(response_data, RecordResponse.class);
}
catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JsonIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Result> results = cashResponse.response.groups;
for (Result cashResult : results){
for(Result_items rest : cashResult.items ){
if(rest!=null && !rest.equals("")){
Log.v("IDSSSSS", rest.name);
Log.v("IDSSSSS", rest.id);
Log.v("IDSSSSS", rest.location.distance);
Log.v("IDSSSSS", rest.location.lat);
Log.v("IDSSSSS", rest.location.lng);
try{
for(Result_category cat : rest.categories ){
Log.v("category_icon", cat.id);
Log.v("category_name", cat.icon);
Log.v("category_id", cat.name);
}
}catch(Exception e){}
}
}
}
用于获取数据的相应类文件:
class RecordResponse{
CashGamesContainer response;
}
class CashGamesContainer{
List<Result> groups;
}
class Result{
List<Result_items> items;
}
class Result_items{
String id;
String name;
String verified;
Locations location;
Stats stats;
List<Result_category> categories;
}
class Locations{
String address;
String lat;
String lng;
String distance;
}
class Stats{
String checkinsCount;
String usersCount;
String tipCount;
}
class Result_category{
String id;
String name;
String icon;
}