i'm working on deserialization of feed coming from server. I have encountered a weird issue with Gson library. I'm using code i've found here on stack on similar issue thread, but it doesn't work.
我正在进行来自服务器的反序列化反序列化。我在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"
}]
Unfortunately i don't know where the problem is... Can someone point me in the right direction?
不幸的是,我不知道问题出在哪里......有人能指出我正确的方向吗?
public static ArrayList<FeedItem> parseFeed(String toParse){
Type collectionType = new TypeToken<Collection<FeedItem>>(){}.getType();
ArrayList<FeedItem> outcome = new Gson().fromJson(toParse, collectionType);
return outcome;
}
Error
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)
2 个解决方案
#1
Your Json array format is not correct because of that you are getting the above exception. You have a lot of extra properties and some of your properties doesnt match the model types:
您的Json数组格式不正确,因为您获得了上述异常。您有许多额外的属性,并且您的某些属性与模型类型不匹配:
Code to generate Json format:
生成Json格式的代码:
public static String parseFeed(ArrayList<FeedItem> list) {
Type collectionType = new TypeToken<Collection<FeedItem>>() {
}.getType();
String outcome = new Gson().toJson(list, collectionType);
return outcome;
Valid Format:
[{"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}]
#2
Error occurred because of the root object, Since your root object doesn't contain an direct array, the array is mapped by the "feeds". So you need to create another Bean object for accessing those array.
由于根对象而发生错误,由于您的根对象不包含直接数组,因此数组由“feeds”映射。因此,您需要创建另一个Bean对象来访问这些数组。
public class FeedsList {
List<FeedItem> feeds;
String timestamp;
}
Now the json can be parsed by using
现在可以使用解析json了
FeedsList feedsList = new Gson().fromJson(jsonSample, FeedsList.class);
But still you need to modify the data types of some variables in FeedItem
because id
, loc
, expireAt
.. etc are declared as String
's in FeedItem
but in the given json they are declared as JsonObject
's
但仍然需要修改FeedItem中某些变量的数据类型,因为id,loc,expireAt等在FeedItem中声明为String,但在给定的json中它们被声明为JsonObject的
#1
Your Json array format is not correct because of that you are getting the above exception. You have a lot of extra properties and some of your properties doesnt match the model types:
您的Json数组格式不正确,因为您获得了上述异常。您有许多额外的属性,并且您的某些属性与模型类型不匹配:
Code to generate Json format:
生成Json格式的代码:
public static String parseFeed(ArrayList<FeedItem> list) {
Type collectionType = new TypeToken<Collection<FeedItem>>() {
}.getType();
String outcome = new Gson().toJson(list, collectionType);
return outcome;
Valid Format:
[{"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}]
#2
Error occurred because of the root object, Since your root object doesn't contain an direct array, the array is mapped by the "feeds". So you need to create another Bean object for accessing those array.
由于根对象而发生错误,由于您的根对象不包含直接数组,因此数组由“feeds”映射。因此,您需要创建另一个Bean对象来访问这些数组。
public class FeedsList {
List<FeedItem> feeds;
String timestamp;
}
Now the json can be parsed by using
现在可以使用解析json了
FeedsList feedsList = new Gson().fromJson(jsonSample, FeedsList.class);
But still you need to modify the data types of some variables in FeedItem
because id
, loc
, expireAt
.. etc are declared as String
's in FeedItem
but in the given json they are declared as JsonObject
's
但仍然需要修改FeedItem中某些变量的数据类型,因为id,loc,expireAt等在FeedItem中声明为String,但在给定的json中它们被声明为JsonObject的