我正在开发应用程序,其中我使用Gson解析序列化json对象,其中包含一些数据的数组..但这里有一些问题我解析的方法...它没有让我成功解析..我从来没有尝试使用序列化注释解析带有Gson库的json数据。请告诉我,我的错误在哪里...
这里是代码..
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
GetAllAssetsResult response = gson.fromJson(reader, GetAllAssetsResult.class);
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
这里是Serialize annotaion base Pojo:
package com.DemoGsonLib;
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class GetAllAssetsResult {
public List<GetAllAssetsResult> getAllAssetsResult;
@SerializedName("ATCOCode")
public String ATCOCode;
@SerializedName("AssetID")
public String AssetID;
@SerializedName("Bearing")
public String Bearing;
@SerializedName("CommonName")
public String CommonName;
@SerializedName("Locality")
public String Locality;
@SerializedName("Location")
public String Location;
@SerializedName("SMSNumber")
public String SMSNumber;
@SerializedName("Street")
public String Street;
}
Json数据:
{
"GetAllAssetsResult": [
{
"ATCOCode": null,
"AssetID": 1,
"Bearing": null,
"CommonName": null,
"Locality": null,
"Location": null,
"SMSNumber": "Little Clare",
"Street": null
},
{
"ATCOCode": "34000000002",
"AssetID": 2,
"Bearing": null,
"CommonName": "Gloucester Green Bus Station",
"Locality": "Oxford City Centre",
"Location": "50.4913",
"SMSNumber": "oxfagwdw",
"Street": "Gloucester Green"
},
{
错误
11-16 13:16:26.805: WARN/System.err(1762): java.lang.NullPointerException
11-16 13:16:26.805: WARN/System.err(1762): at com.DemoGsonLib.DemoGsonLibActivity.onCreate(DemoGsonLibActivity.java:39)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.Activity.performCreate(Activity.java:4533)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-16 13:16:26.805: WARN/System.err(1762): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 13:16:26.805: WARN/System.err(1762): at android.os.Looper.loop(Looper.java:137)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.main(ActivityThread.java:4517)
11-16 13:16:26.805: WARN/System.err(1762): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 13:16:26.805: WARN/System.err(1762): at java.lang.reflect.Method.invoke(Method.java:511)
11-16 13:16:26.805: WARN/System.err(1762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
11-16 13:16:26.805: WARN/System.err(1762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
11-16 13:16:26.805: WARN/System.err(1762): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您没有考虑到需要在您的pojo中序列化Json数组的事实
以下代码应该可以使用
public class GetAllAssetsResult
{
@SerializedName("GetAllAssetsResult")
public List<Asset> assets;
class Asset
{
@SerializedName("ATCOCode")
public String ATCOCode;
@SerializedName("AssetID")
public String AssetID;
@SerializedName("Bearing")
public String Bearing;
@SerializedName("CommonName")
public String CommonName;
@SerializedName("Locality")
public String Locality;
@SerializedName("Location")
public String Location;
@SerializedName("SMSNumber")
public String SMSNumber;
@SerializedName("Street")
public String Street;
}
}