2011-11-02 114 views
10

我想反序列化一個JSON響應,並得到值不能爲空錯誤。反序列化錯誤:值不能爲空。參數名稱:類型

任何幫助真的很感激!我以這種方式反序列化了很多其他的json字符串,並且從未遇到過這個錯誤。我不確定是什麼原因造成的。謝謝!

下面是對象的代碼:

[Serializable] 
public class LocationResponse 
{ 
    public string authenticationResultCode { get; set; } 
    public string brandLogoUri { get; set; } 
    public string copyright { get; set; } 
    public List<ResourceSet> resourceSets { get; set; } 
    public int statusCode { get; set; } 
    public string statusDescription { get; set; } 
    public string traceId { get; set; } 
} 

[Serializable] 
public class ResourceSet 
{ 
    public int estimatedTotal { get; set; } 
    public List<Resource> resources { get; set; } 
} 

[Serializable] 
public class Resource 
{ 
    //public string __type { get; set; } 
    //public List<double> bbox { get; set; } 
    public string name { get; set; } 
    public Point point { get; set; } 
    //public Address address { get; set; } 
    //public string confidence { get; set; } 
    //public string entityType { get; set; } 
} 

[Serializable] 
public class Point 
{ 
    public string type { get; set; } 
    public List<double> coordinates { get; set; } 
} 

[Serializable] 
public class Address 
{ 
    public string countryRegion { get; set; } 
    public string formattedAddress { get; set; } 
} 

代碼反序列化:我得到一個錯誤

System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer(); 
string json = "{\"authenticationResultCode\":\"ValidCredentials\",\"brandLogoUri\":\"http:\\/\\/dev.virtualearth.net\\/Branding\\/logo_powered_by.png\",\"copyright\":\"Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.\",\"resourceSets\":[{\"estimatedTotal\":1,\"resources\":[{\"__type\":\"Location:http:\\/\\/schemas.microsoft.com\\/search\\/local\\/ws\\/rest\\/v1\",\"bbox\":[33.177484847720336,35.531577579036423,33.235425613705445,35.623878963932327],\"name\":\"Qiryat Shemona, Israel\",\"point\":{\"type\":\"Point\",\"coordinates\":[33.206455230712891,35.577728271484375]},\"address\":{\"adminDistrict\":\"Northern\",\"countryRegion\":\"Israel\",\"formattedAddress\":\"Qiryat Shemona, Israel\",\"locality\":\"Qiryat Shemona\"},\"confidence\":\"High\",\"entityType\":\"PopulatedPlace\"}]}],\"statusCode\":200,\"statusDescription\":\"OK\",\"traceId\":\"NVM001351\"}"; 
LocationResponse response = ser.Deserialize<LocationResponse>(json); 

,我想不通的代碼或JSON的哪一部分發生此錯誤:異常詳細信息:System.ArgumentNullException:值不能爲空。 參數名:類型

這裏是堆棧跟蹤,如果這是有幫助的:

[ArgumentNullException: Value cannot be null. 
Parameter name: type] 
System.Activator.CreateInstance(Type type, Boolean nonPublic) +7468694 
System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +406 
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +71 
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147 
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer) +21 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +181 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +119 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +210 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) +422 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +147 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +119 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +210 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) +422 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +147 
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +51 
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +37 
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +70 

回答

15

問題出在JSON中的__type字段。

讀答案如下:Problem with deserializing JSON on datamember 「__type」似乎報價:the "__type" field has a special meaning for DataContractJsonSerializer, denoting the type to which the object should be deserialized.

刪除__type從JSON解決了這個問題。

一個選項(如果您無法控制JSON),我剛剛用JSON.NET庫對其進行了測試,它按預期工作,反序列化並沒有錯誤。

LocationResponse response = JsonConvert.DeserializeObject<LocationResponse>(json); 
+0

非常好!花了很多時間在網上衝浪尋找這個解決方案 – f0rza

0
    ,你可以看到從
  1. 的異常是從System.Activator.CreateInstance拋出(類型類型,布爾)方法堆棧跟蹤。
  2. 由於反序列化程序將「type」作爲「type」傳遞給我上面提到的方法,所以會引發它。

最有可能發生這種情況的原因是反序列化程序無法找到正確的類型來反序列化JSON。嘗試首先序列化LocationResponse類的實例,並將結果與​​要嘗試反序列化的字符串進行比較。

1

這是晚了,但我有同樣的問題,並通過討論添加默認構造函數的類,並確保該類的屬性的制定者是公開的解決它。這解決了我的問題(與FastJson和JSON.net一起提供)。

以防萬一任何人有問題,上面的答案不幫助他們。

相關問題