Json中未定义的行为 [英] Undefined behaviour in Json

查看:84
本文介绍了Json中未定义的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在json字符串中将属性的值设置为undefined时,会发生什么应该. 即:

What should happen when the value of a property is set to undefined in a json string. i.e:

{"Name":undefined}

以下示例正在使用json.net库.反序列化对象时会引发异常.

The following example is using the json.net library. An exception is thrown when de-serializing the object.

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

推荐答案

我认为该错误很好.

Jsonlint.org也会引发错误.

Jsonlint.org throws an error too.

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

And reading the documentation on json.org the "value" element may have the following variants:

字符串 数字 目的 大批 真的 错误的 空

string number object array true false null

如您所见,未列出未定义.对象也不会算作未定义.

As you can see, undefined is NOT listed. Object does also not count as undefined.

这篇关于Json中未定义的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆