如何在JsonConvert.DeserializeObject中处理null/empty值 [英] How to handle null/empty values in JsonConvert.DeserializeObject
本文介绍了如何在JsonConvert.DeserializeObject中处理null/empty值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
return (DataTable)JsonConvert.DeserializeObject(_data, (typeof(DataTable)));
然后,我尝试了:
var jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
return (DataTable)JsonConvert.DeserializeObject<DataTable>(_data, jsonSettings);
返回行抛出错误:
{将值\"\"转换为键入'System.Double'时出错.}
许多在线解决方案建议使用可空类型创建自定义Class
,但这对我不起作用.我不能指望json是某种格式.我无法控制列数,列类型或列名.
Lots of solutions online suggesting creating custom Class
with nullable types but this won't work for me. I can't expect the json to be in a certain format. I have no control over the column count, column type, or column names.
推荐答案
您可以为JsonConvert.DeserializeObject
提供设置,以告诉它如何处理空值(在这种情况下,还有更多):
You can supply settings to JsonConvert.DeserializeObject
to tell it how to handle null values, in this case, and much more:
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var jsonModel = JsonConvert.DeserializeObject<Customer>(jsonString, settings);
这篇关于如何在JsonConvert.DeserializeObject中处理null/empty值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文