0

当属性的值在 json 字符串中设置为 undefined 时会发生什么。IE:

{"Name":undefined}

以下示例使用 json.net 库。反序列化对象时抛出异常。

JsonConvert.DeserializeObject<SimpleObject>("{\"Name\":undefined}");

public class SimpleObject
{
  public string Name { get; set; }
}

Newtonsoft.Json.JsonReaderException was unhandled
  Message=Error reading string. Unexpected token: Undefined. Path 'Value', line 1, position 18.
  Source=Newtonsoft.Json
  LineNumber=1
  LinePosition=18
  Path=Value
4

1 回答 1

1

我认为错误很好。

Jsonlint.org 也会引发错误。

阅读 json.org 上的文档,“value”元素可能有以下变体:

字符串数字对象数组 true false null

如您所见,未列出未定义。对象也不算未定义。

于 2012-05-04T20:30:55.747 回答