问题描述
以下字符串中是否包含有效的JSON?
Are following strings a valid JSON?
"ok"
false
true
null
123
如果不是,为什么标准javascript JSON.parse
方法允许将此值用作有效JSON?
If not why do standard javascript JSON.parse
method allow to use this values as valid JSON?
在实现JSON REST API时,有时会使用此值,并且面对Objective-C框架不会解析此值.
I sometimes use this values when implementing JSON REST APIs, and faced with that objective-c frameworks doesn't parse this values.
推荐答案
严格来说,这些值本身不是有效的JSON,这将需要对象包装器{}
(以及键值对的配置)或数组它们周围的包装器[]
.但是,有许多JSON序列化库允许使用非嵌套原语,如您所示.最好不要依赖此行为,除非您特别知道两端都将使用哪个JSON序列化/反序列化库,并且可以验证它们都以相同的方式支持此行为.
Strictly speaking, those values by themselves are not valid JSON, which would require either an object wrapper {}
(and configuration as key-value pair) or array wrapper []
around them. There are however many JSON serialization libraries that allow for use of non-nested primitives as you have shown. It is probably best to not rely on this behavior unless you specifically know which JSON serialization/deserialization libraries will be in use on both ends and can verify that they both support this behavior in the same manner.
这篇关于值为"ok",false,true,null,123有效的JSON吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!