使用 gson 反序列化对象,预计 BEGIN_ARRAY 但在第 1 行第 2 列为 BEGIN_OBJECT

Deserializing objects with gson, Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

我正在对来自服务器的提要进行反序列化。我在 Gson 库中遇到了一个奇怪的问题。我正在使用我在类似问题线程的堆栈中找到的代码,但它不起作用。

Class:

package com.lanceit.haito.lanceit.model;

/*t*/

import java.sql.Timestamp;
import java.util.ArrayList;

public class FeedItem {
    private String username;
    private String title;
    private String description;

    private String id;

    private int stage;
    private int category;

    private double cost;

    private boolean completed;

    private ArrayList<Attendant> attendants;

    private Location loc;

    private Timestamp createdAt;
    private Timestamp expireAt;

    public FeedItem(String username, String title, String description, String id, int stage, int category, double cost, boolean completed, ArrayList<Attendant> attendants, Location loc, String createdAt, String expireAt) {
        this.username = username;
        this.title = title;
        this.description = description;
        this.id = id;
        this.stage = stage;
        this.category = category;
        this.cost = cost;
        this.completed = completed;
        this.attendants = attendants;
        this.loc = loc;
        this.createdAt = new Timestamp(Long.valueOf(createdAt));
        this.expireAt = new Timestamp(Long.valueOf(expireAt));
    }

    public String getUsername() {
        return username;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}

Json:

    [{
    "username": "q",
    "title": "123213321213",
    "description": "213",
    "loc": {
        "type": "Point",
        "coordinates": [0, 0]
    },
    "category": 0,
    "cost": 123,
    "stage": 0,
    "attendants": [],
    "completed": false,
    "id": "553f4f272300001b004f4078",
    "createdAt": "1430212391230",
    "expireAt": "1437599771230"
}, {
    "username": "q",
    "title": "123213321213",
    "description": "213",
    "loc": {
        "type": "Point",
        "coordinates": [0, 0]
    },
    "category": 0,
    "cost": 123,
    "stage": 0,
    "attendants": [],
    "completed": false,
    "id": "553f4f282300001b004f4079",
    "createdAt": "1430212392752",
    "expireAt": "1437599772752"
}]

不幸的是,我不知道问题出在哪里...有人可以指出正确的方向吗?

public static ArrayList<FeedItem> parseFeed(String toParse){
        Type collectionType = new TypeToken<Collection<FeedItem>>(){}.getType();
        ArrayList<FeedItem> outcome = new Gson().fromJson(toParse, collectionType);
        return outcome;
    }

错误

05-09 13:22:06.575    2554-2554/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.lanceit.haito.lanceit, PID: 2554
com.google.gson.JsonSyntaxException: 1430212391230
        at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:81)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:66)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:526)
        at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:524)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.read(ReflectiveTypeAdapterFactory.java:93)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
        at com.google.gson.Gson.fromJson(Gson.java:803)
        at com.google.gson.Gson.fromJson(Gson.java:768)
        at com.google.gson.Gson.fromJson(Gson.java:717)
        at com.lanceit.haito.lanceit.utils.SerializationHelper.parseFeed(SerializationHelper.java:55)
        at com.lanceit.haito.lanceit.network.lanceHandler.ListAllLances.onResponse(ListAllLances.java:39)
        at com.lanceit.haito.lanceit.network.lanceHandler.ListAllLances.onResponse(ListAllLances.java:29)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.text.ParseException: Unparseable date: "1430212391230" (at offset 13)
        at java.text.DateFormat.parse(DateFormat.java:571)
        at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:79)
            at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:66)
            at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41)
            at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:526)
            at com.google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:524)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.read(ReflectiveTypeAdapterFactory.java:93)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
            at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
            at com.google.gson.Gson.fromJson(Gson.java:803)
            at com.google.gson.Gson.fromJson(Gson.java:768)
            at com.google.gson.Gson.fromJson(Gson.java:717)
            at com.lanceit.haito.lanceit.utils.SerializationHelper.parseFeed(SerializationHelper.java:55)
            at com.lanceit.haito.lanceit.network.lanceHandler.ListAllLances.onResponse(ListAllLances.java:39)
            at com.lanceit.haito.lanceit.network.lanceHandler.ListAllLances.onResponse(ListAllLances.java:29)
            at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
            at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

您的 Json 数组格式不正确,因为您遇到了上述异常。您有很多额外的属性,并且您的某些属性与模型类型不匹配:

生成Json格式的代码:

 public static String parseFeed(ArrayList<FeedItem> list) {
        Type collectionType = new TypeToken<Collection<FeedItem>>() {
        }.getType();
        String outcome = new Gson().toJson(list, collectionType);
        return outcome;

有效格式:

[{"id":"id1","title":"title1","description":"desc1","createdAt":"Jan 1, 1970 12:00:00 PM","expireAt":"Jan 1, 1970 12:00:00 PM","lat":1.0,"lng":1.0,"cost":10,"category":1}]

由于根对象发生错误,由于您的根对象不包含直接数组,该数组由 "feeds" 映射。所以你需要创建另一个 Bean 对象来访问这些数组。

public class FeedsList {
    List<FeedItem> feeds;
    String timestamp;
}

现在可以使用

解析 json
 FeedsList feedsList = new Gson().fromJson(jsonSample, FeedsList.class);

但是还是需要修改FeedItem中的一些变量的数据类型,因为idlocexpireAt..等被声明为StringFeedItem中,但在给定的json中,它们被声明为JsonObject