2011-11-02 6 views
10

json 응답을 deserialize하려면 노력하고있어 값을 가져 오는 중 null 오류가 될 수 없습니다.직렬화 복원 오류 : 값은 null 일 수 없습니다. 매개 변수 이름 : 형식

도움이 정말 감사합니다! 나는이 방법으로 많은 json 문자열을 deserialize하고 있으며 결코이 오류가 발생하지 않았습니다. 나는 그것을 일으키는 것이 확실하지 않습니다. 감사!

[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 : 값을 null 일 수 없습니다. 매개 변수 이름 : 그 도움이 경우 유형 여기

는 스택 추적입니다 :

[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 필드에 있습니다. 다음에 대한 답을 읽기

은 다음 JSON에서 문제가 해결에서 the "__type" field has a special meaning for DataContractJsonSerializer, denoting the type to which the object should be deserialized.

__type 제거 : Problem with deserializing JSON on datamember “__type”는 그 견적을 보인다.

(JSON을 제어 할 수없는 경우) JSON.NET 라이브러리를 사용하여이 기능을 테스트했으며 오류없이 비 직렬화로 예상대로 작동했습니다.

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

우수! 이 솔루션을 찾기 위해 웹 서핑을하면서 많은 시간을 보냈습니다. – f0rza

0
    당신이에서 볼 수
  1. 예외가 System.Activator.CreateInstance에서 발생합니다 (유형 유형, 부울) 방법 스택 추적.
  2. 디시리얼라이저가 앞서 언급 한 메서드에 "형식"으로 null을 전달하기 때문에 throw됩니다.

대부분의 경우 deserializer가 JSON을 deserialize 할 적절한 유형을 찾을 수 없기 때문에 발생합니다. 먼저 LocationResponse 클래스의 인스턴스를 serialize하고 deserialize하려는 문자열과 결과를 비교하십시오.

1

이 늦게,하지만 난 같은 문제를 가지고 문제의 클래스에 기본 생성자를 추가하고 해당 클래스의 속성에 대한 세터 공개했다 확인하여 그것을 해결. 이 문제가 해결되었습니다 (FastJson 및 JSON.net 모두 제공).

누구 에게든 문제가있는 경우 위의 답변으로 문제를 해결할 수 있습니다.

관련 문제