问题:使用Jersey进行序列化,使用gson反序列化JSON
问题描述:
我一直在使用Jersey进行REST API并返回JSON。在客户端,我使用google-gson。在反序列化JSON的同时,我遇到了以下错误。问题:使用Jersey进行序列化,使用gson反序列化JSON
com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {} given the type interface java.util.Map
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonDeserializationCo
正在由新泽西序列化到JSON由具有物体非通用的(没有类型指定)地图和列表
public class Dealer implements Serializable
{
private String serviceURL;
private Map hoursService;
List dealerAttributes;
}
在这里是由新泽西州
{"serviceURL":www.google.com,"hoursService":{"3":{"dayOfWeek":3,"closeTime":"6:30 PM","openTime":"7:30 AM"},"2":{"dayOfWeek":2,"closeTime":"6:30 PM","openTime":"7:30 AM"},"1":{"dayOfWeek":1,"closeTime":"6:30 PM","openTime":"7:30 AM"}},"dealerAttributes":[{"language":"English","dealerAttributeName":"Spanish Speaking","updateDate":1174971061000},{"language":"English","updateDate":1103003316000}]}
产生的JSON
可能是什么原因。请帮我解决这个问题。有没有比gson更好的其他工具?请建议。
谢谢。
答
示例JSON无效。 http://jsonlint.com
www.google.com必须附带引号。那么它是有效的。
已纠正了JSON,下面的反序列化例子输出
[Dealer: serviceURL=www.google.com, hoursService={ 1=[Hours: dayOfWeek=1, closeTime=6:30 PM, openTime=7:30 AM], 2=[Hours: dayOfWeek=2, closeTime=6:30 PM, openTime=7:30 AM], 3=[Hours: dayOfWeek=3, closeTime=6:30 PM, openTime=7:30 AM]}, dealerAttributes=[ [DealerAttributes: language=English, dealerAttributeName=Spanish Speaking, updateDate=1174971061000], [DealerAttributes: language=English, dealerAttributeName=null, updateDate=1103003316000]]]
public class Foo
{
static String jsonInput =
"{" +
"\"serviceURL\":\"www.google.com\"," +
"\"hoursService\":" +
"{" +
"\"1\":" +
"{" +
"\"dayOfWeek\":1," +
"\"closeTime\":\"6:30 PM\"," +
"\"openTime\":\"7:30 AM\"" +
"}," +
"\"2\":" +
"{" +
"\"dayOfWeek\":2," +
"\"closeTime\":\"6:30 PM\"," +
"\"openTime\":\"7:30 AM\"" +
"}," +
"\"3\":" +
"{" +
"\"dayOfWeek\":3," +
"\"closeTime\":\"6:30 PM\"," +
"\"openTime\":\"7:30 AM\"" +
"}" +
"}," +
"\"dealerAttributes\":" +
"[" +
"{" +
"\"language\":\"English\"," +
"\"dealerAttributeName\":\"Spanish Speaking\"," +
"\"updateDate\":1174971061000" +
"}," +
"{" +
"\"language\":\"English\"," +
"\"updateDate\":1103003316000" +
"}" +
"]" +
"}";
public static void main(String[] args)
{
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Dealer dealer = gson.fromJson(jsonInput, Dealer.class);
System.out.println(dealer);
}
}
class Dealer
{
private String serviceURL;
private Map<String, Hours> hoursService;
private List<DealerAttributes> dealerAttributes;
@Override
public String toString()
{
return String.format(
"[Dealer: serviceURL=%1$s, hoursService=%2$s, dealerAttributes=%3$s]",
serviceURL, hoursService, dealerAttributes);
}
}
class Hours
{
private int dayOfWeek;
private String closeTime;
private String openTime;
@Override
public String toString()
{
return String.format(
"[Hours: dayOfWeek=%1$d, closeTime=%2$s, openTime=%3$s]",
dayOfWeek, closeTime, openTime);
}
}
class DealerAttributes
{
private String language;
private String dealerAttributeName;
private long updateDate;
@Override
public String toString()
{
return String.format(
"[DealerAttributes: language=%1$s, dealerAttributeName=%2$s, updateDate=%3$d]",
language, dealerAttributeName, updateDate);
}
}
这不使用泛型类型,我明白你说你没有使用序列化,但你坚持相同反序列化时的限制?如果是这样,那么你将不得不实施一些更“手动”的反序列化解析,因为,正如文档所说,"[w]hile deserializing, Collection must be of a specific generic type"。
同类问题:http://*.com/questions/5424263/json-decode-custom-class-with-hashmap-member-using-gson-in-java – MatrixFrog 2011-05-14 18:52:52