可以引用 JSON 数字吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15368231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:48:07  来源:igfitidea点击:

Can JSON numbers be quoted?

jsonstringparsingnumbersquotes

提问by Unreal user

Can there be quotes around JSON numbers? In most of the search links, it seems that numbers do not "require" quotes. But, should the parsers accept both "attr" : 6and "attr" : "6"?

JSON 数字周围可以有引号吗?在大多数搜索链接中,数字似乎并不“需要”引号。但是,解析器是否应该同时接受"attr" : 6"attr" : "6"

If MyParserhas a method getIntto get a number given the key, should MyParser.getInt("attr")return 6in both cases, or throw an exception in the latter case?

如果MyParser有一个方法getInt来获取给定键的数字,应该在两种情况下都MyParser.getInt("attr")返回6,还是在后一种情况下抛出异常?

回答by T.J. Crowder

In JSON, 6is the number six. "6"is a stringcontaining the digit6. So the answer to the question "Can json numbers be quoted?"is basically "no," because if you put them in quotes, they're not numbers anymore.

JSON 中6是数字六。"6"是一个包含数字字符串。所以这个问题的答案“可以引用json数字吗?” 基本上是“不”,因为如果你把它们放在引号中,它们就不再是数字了。6

But, should the parsers accept both "attr" : 6 and attr : "6"?

但是,解析器是否应该同时接受 "attr" : 6 和 attr : "6"?

The second example is invalid, because attrmust be in quotes, e.g.:

第二个例子无效,因为attr必须用引号引起来,例如:

{"attr": "6"}

...is valid, and defines an object with a property called attrwith the stringvalue "6", whereas:

... 是有效的,并定义了一个对象,该对象具有一个attr字符串value调用的属性"6",而:

{"attr": 6}

...is valid, and defines an object with a property called attrwith the numbervalue 6, and finally:

......是有效的,并且定义了一个名为属性的对象attr数量6,最后:

{attr: 6}

...and

...和

{attr: "6"}

...are both invalid JSON because property names must be in double quotes.

...都是无效的 JSON,因为属性名称必须用双引号引起来。

If MyParser has a method getInt to get a number given the key, should MyParser.getInt("attr") return 6 in both the cases or throw an exception in the latter case?

如果 MyParser 有一个方法 getInt 来获取给定键的数字,那么 MyParser.getInt("attr") 在这两种情况下都应该返回 6 还是在后一种情况下抛出异常?

That's a design decision for the person providing the parser, basically the choice between getIntbeing strict (throwing an exception if you try it on "attr": "6") or loose (coercing "6"to 6and returning that). JavaScript is usually loose, and so there could be an argument for being loose; conversely, the fact that JavaScript is loose sometimes causes trouble, which could be an argument for being strict.

这是为个人提供的解析器,基本上之间选择一个设计决策getInt是严格(抛出一个异常,如果你试试"attr": "6")或松散的(胁迫"6"6并返回一个)。JavaScript 通常是松散的,因此可能存在松散的争论;相反,JavaScript 松散的事实有时会导致麻烦,这可能是严格的一个论据。

回答by scubaFun

That will depend of the language that you use to get the integer, because if the programming language does not provide implicit conversion from string to int, you can have problems.

这将取决于您用于获取整数的语言,因为如果编程语言不提供从字符串到 int 的隐式转换,您可能会遇到问题。

You should not to worry too much, since modern programming language nowadays can implicitly convert a string to a number without additional code. Something you should take into consideration is, when using programming languages like JavaScript, when you use ==and ===when comparing values, ===takes into consideration the value's type while ==not, so 6 === "6"will return false, while 6 == "6"will return true.

您不必太担心,因为如今的现代编程语言可以将字符串隐式转换为数字,而无需额外的代码。你应该考虑的是,当使用像 JavaScript 这样的编程语言时,当你使用=====比较值时,===考虑值的类型而==不是,所以6 === "6"会返回false,而6 == "6"会返回true

Answering your question, it will not throw an exception if you are using a programming language that provides implicit conversion from string to int.

回答您的问题,如果您使用的编程语言提供从字符串到 int 的隐式转换,它不会抛出异常。

回答by user6882413

You can quote the number. Both are valid ("attr" : 6 and "attr" : "6") The only thing need to keep in mind that while extracting the value you need to use GetInt() in first case and GetString() in second case and then convert that string to integer.

你可以引用这个数字。两者都是有效的 ("attr" : 6 和 "attr" : "6") 唯一需要记住的是,在提取值时,您需要在第一种情况下使用 GetInt() 并在第二种情况下使用 GetString() 然后将该字符串转换为整数。