反序列化错误:值不能为空.参数名称:类型

本文关键字:参数 类型 错误 不能 反序列化 | 更新日期: 2023-09-27 18:13:27

我正在尝试反序列化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, Israel'",'"locality'":'"Qiryat Shemona'"},'"confidence'":'"High'",'"entityType'":'"PopulatedPlace'"}]}],'"statusCode'":200,'"statusDescription'":'"OK'",'"traceId'":'"NVM001351'"}";
LocationResponse response = ser.Deserialize<LocationResponse>(json);

我得到一个错误,我无法找出代码或json的哪一部分给出这个错误:异常详细信息:系统。ArgumentNullException:值不能为空。参数名称:type

如果有帮助的话,下面是堆栈跟踪:

[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

反序列化错误:值不能为空.参数名称:类型

问题是JSON中的__type字段。

阅读以下问题的答案:在数据成员"__type"上反序列化JSON的问题似乎引用:the "__type" field has a special meaning for DataContractJsonSerializer, denoting the type to which the object should be deserialized.

从JSON中删除__type解决了这个问题。

一个选项,(如果您无法控制JSON),我刚刚用JSON测试了这个。. NET库,它按预期工作,反序列化没有错误。

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

这有点晚了,但我遇到了同样的问题,并通过向有问题的类添加默认构造函数并确保该类属性的setter是公共的来解决这个问题。这解决了我的问题(FastJson和JSON.net都存在)。

以防有人有问题,上面的答案不能帮助他们。

    从System.Activator抛出异常。创建实例(类型类型,bool)方法,可以从堆栈跟踪中看到。
  1. 抛出是因为反序列化器将null作为"类型"传递给我上面提到的方法。

很可能是因为反序列化器无法找到正确的类型来反序列化JSON。尝试先序列化您的LocationResponse类的实例,并将结果与您试图反序列化的字符串进行比较。