0

我使用 GSON 解析 JSON 数据,我的应用程序目前能够解析 Google Geocoder。但是,每当我尝试从另一个网站解析 JSON 时,它都会给出 nullpointerexception

这是我的 EventResponse 类,它连接了 GSON 和网站

public class EventResponse {
    public Meta meta;
    public Objects[] Objects;

    public EventResponse() {}

    public class Meta {
        public int limit;
        public String next;
        public int offset;
        public String previous;
        public int total_count;
    }

    public class Objects {
        public String category;
        public String date;
        public String description;
        public int id;
        public String name;
        public String resource_uri;
        public String venue;
    }
}

这是 JSON

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 7
    },
    "objects": [
        {
            "category": "/api/v1/category/1/",
            "date": "2013-05-01",
            "description": "Beş yakın arkadaş, ", //Description is longer in real
            "id": 10,
            "name": "Kötü Ruh / Evil Dead",
            "resource_uri": "/api/v1/event/10/",
            "venue": "/api/v1/venue/3/"
        },
        {
            "category": "/api/v1/category/1/",
            "date": "2013-05-02",
            "description": "Filmde, küçümseyen, ",
            "id": 9,
            "name": "Korkunç Bir Film 5 / Scary Movie 5",
            "resource_uri": "/api/v1/event/9/",
            "venue": "/api/v1/venue/6/"
        }
    ]
}

这就是我使用 GSON 的方式(我也尝试过 Objects[0],没关系)

Gson gson = new Gson();
        EventResponse eResult = new EventResponse();
        try {
            eResult = gson.fromJson(jsonCoord((catURL + Categories.catID)),
                    EventResponse.class);

            Log.i("ama"+eResult.Objects[1].category, "deneme");

jsonCoord 方法

public String jsonCoord(String address) throws IOException {
        URL url = new URL(address);
        URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String inputLine;
        String jsonResult = "";
        while ((inputLine = in.readLine()) != null) {
            jsonResult += inputLine;
        }
        in.close();
        return jsonResult;
    }

即使从日志中我也无法获得任何价值。错误堆栈是这样的:(第59行是日志行)

05-02 21:48:32.020: W/System.err(17456): java.lang.NullPointerException
05-02 21:48:32.030: W/System.err(17456):    at com.myapp.myapp.Events.getVenue(Events.java:59)
05-02 21:48:32.030: W/System.err(17456):    at com.myapp.myapp.Events.onCreate(Events.java:48)
05-02 21:48:32.030: W/System.err(17456):    at android.app.Activity.performCreate(Activity.java:5206)
05-02 21:48:32.030: W/System.err(17456):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-02 21:48:32.030: W/System.err(17456):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-02 21:48:32.030: W/System.err(17456):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-02 21:48:32.030: W/System.err(17456):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-02 21:48:32.030: W/System.err(17456):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-02 21:48:32.030: W/System.err(17456):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 21:48:32.030: W/System.err(17456):    at android.os.Looper.loop(Looper.java:137)
05-02 21:48:32.030: W/System.err(17456):    at android.app.ActivityThread.main(ActivityThread.java:4898)
05-02 21:48:32.030: W/System.err(17456):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 21:48:32.030: W/System.err(17456):    at java.lang.reflect.Method.invoke(Method.java:511)
05-02 21:48:32.030: W/System.err(17456):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-02 21:48:32.035: W/System.err(17456):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-02 21:48:32.035: W/System.err(17456):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

3

正如已经指出的那样,您的问题只是该字段的名称。

在您的班级中,名称是“Objects”,但在您的 JSON 响应中,它是“objects”(小写)。您的程序和 JSON 中的变量名称必须完全相同,因为 GSON 允许您从 JSON 响应中仅获取您感兴趣的那些字段......所以如果它在响应中找到一个 JSON 值与您的任何类属性都不对应,GSON 只是忽略该值。

您可以通过以下方式使用注释@SerializedName

@SerializedName("objects")
private Objects[] whateverName;

这样,您可以在类和 JSON 响应中使用不同的名称。

注意:属性的类型与您的问题没有任何关系。您的类Objects应以大写形式编写以遵循 Java 命名约定...

于 2013-05-02T21:19:50.747 回答
2

问题是 EventResponse 类中的对象写错了

使它看起来像这个已解决的问题

public class EventResponse {
    public Meta meta;
    public objects[] objects; //I also changed the type for it not to have any
trouble.

    public EventResponse() {}

    public class Meta {
        public int limit;
        public String next;
        public int offset;
        public String previous;
        public int total_count;
    }

    public class objects {
        public String category;
        public String date;
        public String description;
        public int id;
        public String name;
        public String resource_uri;
        public String venue;
    }
}
于 2013-05-02T20:30:33.300 回答