gpt4 book ai didi

json - 单个独立文字能否形成有效的 JSON "document"?

转载 作者:行者123 更新时间:2023-12-01 10:51:52 25 4
gpt4 key购买 nike

例如,这应该是一个有效的 JSON 文档吗?

"foo"

json.org 上的语法规范并不完全清楚。我不认为规范中的任何地方都说所有内容都必须在有效 JSON 文档中的 {} 对象或 [] 数组中。

JSONLint 将独立字符串 "foo" 标记为错误,并期望所有内容都在 {} 对象或 [] 数组。

然而,主流浏览器(IE 8、IE 10、Chrome 28、Firefox 23、Opera 12)的 JSON 对象接受独立文字就好了:

>>> JSON.parse('"foo"');
"foo"
>>> JSON.parse('true');
true
>>> JSON.parse('1234');
1234

Python 2.7+ 也一样:

>>> import json
>>> json.loads('"foo"')
u'foo'
>>> json.loads('true')
True
>>> json.loads('1234')
1234

那么谁对谁错呢?

最佳答案

在评论中找到这个

Actually there are two different JSON specifications. RFC 4627 requires a JSON text to be an object or an array. ECMA-262, 5th edition, section 15.12 does not impose this restriction.

JSON root element

关于json - 单个独立文字能否形成有效的 JSON "document"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com