我有一个Java类,用户:如何为Gson编写自定义JSON解串器?
public class User
{
int id;
String name;
Timestamp updateDate;
}
我收到包含来自web服务的用户对象上JSON列表:
[{"id":1,"name":"Jonas","update_date":"1300962900226"},
{"id":5,"name":"Test","date_date":"1304782298024"}]
我曾尝试编写自定义解串器:
@Override
public User deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
return new User(
json.getAsJsonPrimitive().getAsInt(),
json.getAsString(),
json.getAsInt(),
(Timestamp)context.deserialize(json.getAsJsonPrimitive(),
Timestamp.class));
}
但我的反序列化器不起作用。我怎样才能为Gson编写一个定制的JSON解串器?
这是否例如帮助呢? http://stackoverflow.com/questions/5845822/gson-deserializing-key-value-to-custom-object – 2011-05-23 12:17:33
你是什么意思它“不起作用”?有什么问题? – ColinD 2011-05-23 13:25:52
“date_date”应该是“update_date”吗? – 2011-06-01 17:39:03