反序列化 JSON 时,值不能为空
本文关键字:不能 JSON 反序列化 | 更新日期: 2023-09-27 17:56:31
我有一个数据库表,其中包含以 JSON 格式存储内容的列,我正在检索 JSON,但收到此错误。
值不能为空
这是我的代码片段。
var stuff = (JObject)JsonConvert.DeserializeObject(commentId);
string name = stuff["Name"].Value<string>();
string email = stuff["Email"].Value<string>();
string company = stuff["Company"].Value<string>();
string phone = stuff["Phone"].Value<string>();
string message = stuff["Message"].Value<string>();
string emails = stuff["Emails"].Value<string>();
我的示例 JSON:
{
"2": {
"label": "",
"value": "",
"type": null,
"validation": null,
"required": null,
"min": null,
"max": null,
"tooltip": null,
"custom": null,
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"3": {
"label": "location",
"value": "http://someurl.com/",
"type": "hidden",
"validation": "",
"required": "0",
"min": "0",
"max": "1000",
"tooltip": "",
"custom": null,
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"4": {
"label": "Name*",
"value": "Farrukh",
"type": "text",
"validation": "alphabets",
"required": "1",
"min": "0",
"max": "300",
"tooltip": "field0",
"custom": "",
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"5": {
"label": "Email*",
"value": "abc@a.com",
"type": "email",
"validation": "email",
"required": "1",
"min": "",
"max": "",
"tooltip": "field1",
"custom": "autoreply",
"custom2": "replyto",
"custom3": "zz",
"custom4": "",
"custom5": ""
},
"6": {
"label": "Company",
"value": "Abc",
"type": "text",
"validation": "",
"required": "1",
"min": "0",
"max": "300",
"tooltip": "field2",
"custom": "",
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"7": {
"label": "Phone",
"value": "0000000000",
"type": "text",
"validation": "integers",
"required": "0",
"min": "0",
"max": "300",
"tooltip": "field3",
"custom": "",
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"8": {
"label": "Country",
"value": "Some country",
"type": "dropdown",
"validation": "",
"required": "1",
"min": "",
"max": "",
"tooltip": "field4",
"custom": null,
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"9": {
"label": "Message*",
"value": "hello",
"type": "para",
"validation": "",
"required": "1",
"min": "0",
"max": "3000",
"tooltip": "field5",
"custom": "",
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"10": {
"label": "name",
"value": "",
"type": null,
"validation": null,
"required": null,
"min": null,
"max": null,
"tooltip": null,
"custom": null,
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"11": {
"label": "title",
"value": "",
"type": null,
"validation": null,
"required": null,
"min": null,
"max": null,
"tooltip": null,
"custom": null,
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"12": {
"label": "emails",
"value": ",a@a.com,b@b.com,c@c.com,d@d.com",
"type": null,
"validation": null,
"required": null,
"min": null,
"max": null,
"tooltip": null,
"custom": null,
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"13": {
"label": "multi",
"value": "true",
"type": null,
"validation": null,
"required": null,
"min": null,
"max": null,
"tooltip": null,
"custom": null,
"custom2": null,
"custom3": "zz",
"custom4": null,
"custom5": null
},
"0": {
"custom3": "zz",
"value": "",
"label": ""
},
"1": {
"custom3": "zz",
"value": "",
"label": ""
}
}
我的猜测是,当你这样做时
string name = stuff["Name"].Value<string>();
和其他人,你正在获取东西["名称"]的值,如果它在 JSON 中没有该属性,对于此示例,它没有"名称",它会抛出"值不能为空"
因此,您首先需要检查是否有值。
你可以这样检查
//check if property exists
if (stuff["Name"].Value<string>()!= null) {
string name = stuff["Name"].Value<string>();
} else {
//there is no "name" property, compensate somehow.
}
假设您的完整 JSON 如图所示,您的 JSON 不会为名称和电子邮件提供简单的名称/值对。 相反,它有一个索引属性对象的字典,其中每个对象都有一个值等于所查找属性名称的label
属性,以及一个具有相应值的相邻value
属性。
您可以通过构建辅助查找表方便地从 JSON 中获取这些内容,如下所示:
var dict = JObject.Parse(commentId)
.Descendants()
.OfType<JProperty>()
.Where(p => p.Name == "label")
.ToLookup(p => (string)p.Value, p => (string)p.Parent["value"]); // Use ToLookup because some empty space keys are duplicated
var name = dict["Name*"].SingleOrDefault(); // Notice the asterisk in the property labels.
var email = dict["Email*"].SingleOrDefault();
并且,要测试:
Debug.Assert(name == "Farrukh"); // No assert.
Debug.Assert(email == "abc@a.com"); // No assert.
很老的话题,但我正在使用:
string name = stuff["Name"]?.Value<string>() ?? "undefined";
string email = stuff["Email"]?.Value<string>() ?? "undefined";
简洁明了,还提供您自己的默认值。