我一直在使用Jersey来获取REST API并返回JSON。在客户端,我使用的是google-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
Jersey被序列化为JSON的对象具有非泛型(未指定类型)Map和List
public class Dealer implements Serializable
{
private String serviceURL;
private Map hoursService;
List dealerAttributes;
}
这是泽西岛生成的JSON
{"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}]}
可能是什么原因。请帮我解决这个问题。有没有比gson更好的其他实用工具?请建议。
感谢。
答案 0 :(得分:2)
示例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"。