簡體   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