2015-05-09 111 views
0

我正在处理来自服务器的反馈序列化。我用Gson库遇到了一个奇怪的问题。我使用的代码是在类似问题的线程上发现的,但它不起作用。使用gson反序列化对象,期望BEGIN_ARRAY,但是BEGIN_OBJECT在第1行第2列

类:

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$22$1.read(TypeAdapters.java:526) 
     at com.google.gson.internal.bind.TypeAdapters$22$1.read(TypeAdapters.java:524) 
     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.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$1.onResponse(ListAllLances.java:39) 
     at com.lanceit.haito.lanceit.network.lanceHandler.ListAllLances$1.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$22$1.read(TypeAdapters.java:526) 
            at com.google.gson.internal.bind.TypeAdapters$22$1.read(TypeAdapters.java:524) 
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.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$1.onResponse(ListAllLances.java:39) 
            at com.lanceit.haito.lanceit.network.lanceHandler.ListAllLances$1.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) 
+0

尝试:'类型collectionType =新TypeToken >(){}。getType();' – Blackbelt

+0

粘贴你的Json字符串最可能是一个格式问题 –

+0

@Blackbelt是的,试了一下。仍然不起作用。 – Haito

回答

1

你的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}] 
+0

好吧..我明白了。你有任何想法如何扁平化该ID? – Haito

+0

用名为$ oid的属性创建一个名为id的独立类,并更新FeedItem以使这个新类型 –

+0

@haito这回答你的问题PLZ标记为答案 –

1

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

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

现在JSON可以通过使用

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

解析但你仍然需要修改一些变量的数据类型FeedItem因为idlocexpireAt ...等被声明为String“ s在FeedItem,但在给定的JSON他们宣布为JsonObject

相关问题