2015-05-09 68 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

相關問題