2011-05-09 130 views
1

我一直在使用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更好的其他工具?請建議。

謝謝。

+0

同類問題:http://stackoverflow.com/questions/5424263/json-decode-custom-class-with-hashmap-member-using-gson-in-java – MatrixFrog 2011-05-14 18:52:52

回答

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"