您好我正在尝试使用gson库解析Json.My Json String就是这个
{
"data": [
{
"atb_atestwebservice_cities": [
{
"id": "3",
"cities": "Ajmer",
"date": "2012-03-01 19:30:32"
}
]
},
{
"atb_atestwebservice_states": [
{
"id": "2",
"states": "goa",
"date": "2012-02-28 12:53:51"
}
]
}
]
} 以下课程
public class Cities {
@SerializedName("id")
private String id;
@SerializedName("cities")
private String cities;
@SerializedName("date")
private String date;
public Cities(String id,String city,String date)
{
this.id=id;
this.date=date;
this.cities=city;
}
public String getId()
{
return id;
}
public String getCity()
{
return cities;
}
public String getDate()
{
return date;
}
} 和
public class States {
@SerializedName("id")
private String id;
@SerializedName("states")
private String states;
@SerializedName("date")
private String date;
public States(String id,String states,String date)
{
this.id=id;
this.date=date;
this.states=states;
}
public String getId()
{
return id;
}
public String getCity()
{
return states;
}
public String getDate()
{
return date;
}
}
和数据类
public class Data {
private List<Cities> atb_atestwebservice_cities;
private List<States> atb_atestwebservice_states;
public Data(List<Cities> cityList,List<States>stateList)
{
this.atb_atestwebservice_cities=cityList;
this.atb_atestwebservice_states=stateList;
}
public List<Cities> getCityList()
{
return atb_atestwebservice_cities;
}
public void setCityList(List<Cities> cityList)
{
this.atb_atestwebservice_cities=cityList;
}
public List<States> getSateList()
{
return atb_atestwebservice_states;
}
public void setDatList(List<States> stateList)
{
this.atb_atestwebservice_states=stateList;
}
} 和反序列化json字符串的代码
Gson gson = new Gson();
Data data = gson.fromJson(jsonString,Data.class);
问题是我得到空指针exception.i已经引用了这个link进行解析。我不确定是什么问题。我的POJO类是正确的还是我需要修改或添加更多类? 编辑:这是我的原木猫
03-14 14:59:31.694: ERROR/AndroidRuntime(323): java.lang.NullPointerException
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at com.jimesh.ShowData$LongOperation.onPostExecute(ShowData.java:88)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at com.jimesh.ShowData$LongOperation.onPostExecute(ShowData.java:1)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at android.os.Looper.loop(Looper.java:123)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at java.lang.reflect.Method.invoke(Method.java:521)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-14 14:59:31.694: ERROR/AndroidRuntime(323): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您的Data.class没有正确编写以正确解析您的json数据。您需要以下结构才能获得所有响应。
您需要使用以下类来获取结果数据。
Gson gson = new Gson();
Data data = gson.fromJson(jsonString,Data.class);
这是您的数据类
public class Data {
@SerializedName("data")
public List<ResultData> mResultData;
}
public class ResultData {
@SerializedName("atb_atestwebservice_cities")
public List<City> mCityList;
@SerializedName("atb_atestwebservice_states")
public List<State> mStateList;
}
public class City {
/*"id": "3",
"cities": "Ajmer",
"date": "2012-03-01 19:30:32"*/
@SerializedName("id")
public String id;
@SerializedName("cities")
public String mCitie;
@SerializedName("date")
public String mData;
}
public class State {
/*"id": "2",
"states": "goa",
"date": "2012-02-28 12:53:51"*/
@SerializedName("id")
public String id;
@SerializedName("states")
public String mState;
@SerializedName("date")
public String mDate;
}
您需要在包中添加所有这些课程。