我试图反序列化一个json响应,并获得该值不能为null的错误.
任何帮助真的很感激!我以这种方式反序列化了很多其他的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,\"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,Object& convertedObject) +71 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Object& convertedObject) +147 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o,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,Int32 depthLimit) +37 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +70
解决方法
问题是JSON中的__type字段.
阅读以下答案:Problem with deserializing JSON on datamember “__type”似乎引用:“__type”字段对DataContractJsonSerializer有一个特殊的含义,表示对象应该反序列化的类型.
一个选项,(如果您无法控制JSON),我刚刚使用JSON.NET库进行了测试,并且按预期工作,反序列化,没有任何错误.
LocationResponse response = JsonConvert.DeserializeObject<LocationResponse>(json);