JSON deserialize , Error : null to value type, how to know exact property causing error?
JSON deserialize , Error : null to value type, how to know exact property causing error?
在我的 C# 代码中,我试图用 100 多个属性 (复杂的、原始的、派生的)反序列化一个 JSON,但出现错误Cannot convert null to a value type.
虽然我终于通过手动故障排除知道是哪个 属性 引起了问题。
但是有什么方法可以让我一次性知道 JSON 或 Result_TYPE property or properties
(导致问题的原因?
我尝试查看异常的 detail window
,但我只能知道 datatype
。就我而言,是 null
试图转换为 boolean
,但未找到 属性 名称。
例如:我的JSON
{
"customerData":
{
//... other json data
"someClass":{
"someClassProp1":"prop1Value",
"someClassProp2":"prop2Value"
},
"isExistData":null,
"someOtherClass":null
//... some other json data
}
}
和Result_TYPE是:
Public class CustomerData
{
// Other properties
public SomeClass someClass:
public bool isExistData;
public SomeOtherClass someOtherClass:
// some Other properties
}
I'm using JavaScriptSerializer().Deserialize<T>(jsonString);
在上面的示例中:我怎么知道 属性 isExistData
会导致反序列化错误,因为 属性 类型是 boolean
并且传入数据是 null
. [当然除了手动调试,因为可能有 100 多个属性]
任何人,如果知道找到确切位置的更好方法 属性?
如果您不介意使用其他序列化器,那么只需使用 JSON .NET,它允许您在反序列化时出错时运行自定义代码:
var errors = new List<string>();
var data = JsonConvert.DeserializeObject<CustomerData>(jsonString,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg)
{
errors.Add(earg.ErrorContext.Member.ToString());
earg.ErrorContext.Handled = true;
}
});
在错误中,您将拥有所有有问题的属性。当然 JSON .NET 默认情况下不会在 null 属性上失败,这就是我设置 JsonSerializerSettings 的 NullValueHandling 属性 的原因。您可以在文档中阅读更多内容:http://www.newtonsoft.com/json/help/html/SerializationErrorHandling.htm
如果出于任何原因您必须继续使用 JavaScriptSerializer,那么只需将您的对象反序列化为动态对象 (Deserialize JSON into C# dynamic object?),然后检查是否有任何值类型的属性不具有 null 值:
foreach (var property in typeof(CustomerData).GetProperties().Where(p => p.PropertyType.IsValueType))
{
if (dynamicsData[property.Name] == null)
{
Console.WriteLine($"This is problematic property: {property.Name}");
}
}
在我的 C# 代码中,我试图用 100 多个属性 (复杂的、原始的、派生的)反序列化一个 JSON,但出现错误Cannot convert null to a value type.
虽然我终于通过手动故障排除知道是哪个 属性 引起了问题。
但是有什么方法可以让我一次性知道 JSON 或 Result_TYPE property or properties
(导致问题的原因?
我尝试查看异常的 detail window
,但我只能知道 datatype
。就我而言,是 null
试图转换为 boolean
,但未找到 属性 名称。
例如:我的JSON
{
"customerData":
{
//... other json data
"someClass":{
"someClassProp1":"prop1Value",
"someClassProp2":"prop2Value"
},
"isExistData":null,
"someOtherClass":null
//... some other json data
}
}
和Result_TYPE是:
Public class CustomerData
{
// Other properties
public SomeClass someClass:
public bool isExistData;
public SomeOtherClass someOtherClass:
// some Other properties
}
I'm using
JavaScriptSerializer().Deserialize<T>(jsonString);
在上面的示例中:我怎么知道 属性 isExistData
会导致反序列化错误,因为 属性 类型是 boolean
并且传入数据是 null
. [当然除了手动调试,因为可能有 100 多个属性]
任何人,如果知道找到确切位置的更好方法 属性?
如果您不介意使用其他序列化器,那么只需使用 JSON .NET,它允许您在反序列化时出错时运行自定义代码:
var errors = new List<string>();
var data = JsonConvert.DeserializeObject<CustomerData>(jsonString,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg)
{
errors.Add(earg.ErrorContext.Member.ToString());
earg.ErrorContext.Handled = true;
}
});
在错误中,您将拥有所有有问题的属性。当然 JSON .NET 默认情况下不会在 null 属性上失败,这就是我设置 JsonSerializerSettings 的 NullValueHandling 属性 的原因。您可以在文档中阅读更多内容:http://www.newtonsoft.com/json/help/html/SerializationErrorHandling.htm
如果出于任何原因您必须继续使用 JavaScriptSerializer,那么只需将您的对象反序列化为动态对象 (Deserialize JSON into C# dynamic object?),然后检查是否有任何值类型的属性不具有 null 值:
foreach (var property in typeof(CustomerData).GetProperties().Where(p => p.PropertyType.IsValueType))
{
if (dynamicsData[property.Name] == null)
{
Console.WriteLine($"This is problematic property: {property.Name}");
}
}