2011-11-02 24 views
10

Próbuję deserializować odpowiedź jsona i otrzymuję wartość nie może być błąd o wartości null.Błąd deserializacji: wartość nie może być pusta. Nazwa parametru: typ

Każda pomoc jest naprawdę doceniana! W ten sposób deserializuję wiele innych ciągów json i nigdy nie napotkam tego błędu. Nie jestem pewien, co to powoduje. Dzięki!

Oto kod dla obiektu:

[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; } 
} 

Kod deserializacji:

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); 

Dostaję błąd i nie mogę dowiedzieć się, jaka część kodu lub json podaje ten błąd: Szczegóły wyjątku: System.ArgumentNullException: Wartość nie może być pusta. Nazwa Parametry: typ

Oto ślad stosu, jeśli jest to pomocne:

[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 

Odpowiedz

15

Problem dotyczy pola __type w JSON.

Czytając odpowiedzi na następujące: Problem with deserializing JSON on datamember “__type” wydaje się, że cytuję: the "__type" field has a special meaning for DataContractJsonSerializer, denoting the type to which the object should be deserialized.

Usuwanie __type z JSON rozwiązany.

Jedna opcja, (jeśli nie masz kontroli nad JSON), właśnie przetestowałem to z biblioteką JSON.NET i działało zgodnie z oczekiwaniami, deserializując bez żadnych błędów.

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

Doskonale! spędził dużo czasu surfując po Internecie, aby znaleźć to rozwiązanie – f0rza

0
  1. jest wyjątek od System.Activator.CreateInstance (typ typ, bool) Sposób jak widać Ślad stosu.
  2. Jest wyrzucany, ponieważ deserializer przekazuje wartość null jako "typ" do metody, o której wspomniałem powyżej.

Najprawdopodobniej dzieje się tak, ponieważ deserializer nie jest w stanie znaleźć odpowiedniego typu, aby deserializować JSON do. Spróbuj serializować instancję klasy LocationResponse i porównać wynik z łańcuchem, który próbujesz deserializować.

1

Jest późno, ale miałem ten sam problem i rozwiązać go poprzez dodanie domyślnego konstruktora do danej klasy i upewniając się, że setery do właściwości tej klasy były publiczne. To rozwiązało mój problem (obecny zarówno w FastJson, jak i JSON.net).

Na wszelki wypadek, gdy ktoś ma problem, a powyższe odpowiedzi nie pomagają.

Powiązane problemy