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

时间:2015-06-16 07:34:58

标签: c# winforms json.net

我有一个db表,其中的内容以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": ""
  }
}

3 个答案:

答案 0 :(得分:1)

我的猜测是,当你做

string name = stuff["Name"].Value<string>();

和其他人你获得了东西的价值[&#34;名称&#34;]如果它没有JSON中的那个属性,对于这个例子它没有&# 34;名称&#34;它会抛出你#34;价值不能为空&#34;

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

你可以这样检查

//check if property exists
if (stuff["Name"].Value<string>()!= null) {
    string name = stuff["Name"].Value<string>();
} else {
    //there is no "name" property, compensate somehow.
}

答案 1 :(得分:1)

假设您的完整JSON正如您所示,您的JSON不会为名称和电子邮件提供简单的名称/值对。相反,它有一个索引属性对象的字典,其中每个对象都有一个label属性,其值等于您搜索的属性的名称,以及一个具有相应值的相邻value属性。

您可以通过构建一个辅助lookup table,方便地从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.

答案 2 :(得分:0)

很老的话题,但是我正在使用:

string name = stuff["Name"]?.Value<string>() ?? "undefined"; string email = stuff["Email"]?.Value<string>() ?? "undefined";

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