繁体   English   中英

反序列化JSON时,值不能为null

[英]Value cannot be null when deserializing JSON

我有一个数据库表,该表具有以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>();

而其他人则获得stuff [“ Name”]的值,如果它在JSON中不具有该属性,则在此示例中,它不具有“ Name”,它将抛出“值不能为空”

因此,您需要首先检查是否具有值。

你可以这样检查

//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";

简洁明了,还提供您自己的默认值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM