简体   繁体   English

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

[英]Value cannot be null when deserializing JSON

I have a db table which has column in which content is stored in JSON format, I am retrieving JSON but I am getting this error. 我有一个数据库表,该表具有以JSON格式存储内容的列,我正在检索JSON,但出现此错误。
Value cannot be null 值不能为空
Here is my code snippet. 这是我的代码段。

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>();

My sample JSON: 我的示例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": ""
  }
}

My guess is that when you do 我的猜测是

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

and the others you are getting the value for stuff["Name"] and if it doesn't have that property in the JSON, for this example it doesn't have "Name" it will throw you "Value cannot be null" 而其他人则获得stuff [“ Name”]的值,如果它在JSON中不具有该属性,则在此示例中,它不具有“ Name”,它将抛出“值不能为空”

So you need first to check if you have the values or not. 因此,您需要首先检查是否具有值。

You can check like that 你可以这样检查

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

Assuming your full JSON is as you show, your JSON doesn't give simple name/value pairs for name and email. 假设您显示的是完整的JSON,那么JSON不会为名称和电子邮件提供简单的名称/值对。 Instead it has a dictionary of indexed property objects, where each object has a label property with value equal to the name of property you seek, and an adjacent value property with the corresponding value. 相反,它具有一个索引属性对象字典,其中每个对象都有一个label属性,其值等于您要查找的属性的名称,以及一个相邻的value属性,具有相应的值。

You can conveniently fetch these from your JSON by building an ancillary lookup table as follows: 通过构建如下的辅助查找表 ,可以方便地从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();

And, to test: 并且,要测试:

        Debug.Assert(name == "Farrukh"); // No assert.
        Debug.Assert(email == "abc@a.com"); // No assert.

Pretty old topic, but I am using: 相当老的话题,但是我正在使用:

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

Concise and simple and also provides your own default value. 简洁明了,还提供您自己的默认值。

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

相关问题 反序列化JSON时遇到Null - Null encountered when deserializing JSON 反序列化JSON时需要忽略NULL值 - Need to ignore NULL values when deserializing JSON 反序列化 json 数组时的 null 属性 - null properties when deserializing json array Null class 反序列化时 Json C# - Null class when deserializing Json C# Newtonsoft.JSON null 反序列化时的值 - Newtonsoft.JSON null values when deserializing 使用自定义 ContractResolver,在将 null JSON 属性反序列化为值类型成员时如何设置默认值而不是 null? - Using a custom ContractResolver, how to set a default value instead of null when deserializing a null JSON property to a value-type member? 反序列化错误-值不能为null /未定义无参数构造函数 - Error Deserializing - Value cannot be null / No parameterless constructor defined System.Text.Json:反序列化时,如何忽略在构造函数中设置了默认值的属性的 null 或空字符串值? - System.Text.Json: when deserializing, how can I ignore a null or empty string value for a property that has a default value set in the constructor? 在反序列化JSON响应时,RestSharp客户端将所有属性返回为null - RestSharp client returns all properties as null when deserializing JSON response 反序列化以前在序列化时包含值的空字段 - Deserializing a null field that previously contained a value when it was serialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM