是否将原始类型视为JSON? [英] Is a primitive type considered JSON?

查看:48
本文介绍了是否将原始类型视为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON的大多数位置都采用类似

Most place the JSON is in format like

{
    color: "red",
    value: "#f00"
}

[  
    { color: "red",     value: "#f00"   },
    { color: "red",     value: "#f00"   }
]

我想问的是,字符串,布尔值,int还是JSON之类的原始类型吗?

And I want to ask is primitive type like string, bool, int also JSON?

我找到了跟踪链接,

http://json-schema.org/latest/json-schema- core.html

http://www.json.org/

https://zh.wikipedia.org/wiki/JSON

https://www.ietf.org/rfc/rfc4627.txt

http://www.c-sharpcorner.com /uploadfile/3d39b4/data-types-in-json/

然后

在RFC4627中说

JSON可以表示四种原始类型(字符串,数字,布尔值, 和null)和两种结构化类型(对象和数组).

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

字符串是零个或多个Unicode字符[UNICODE]的序列.

A string is a sequence of zero or more Unicode characters [UNICODE].

对象是零个或多个名称/值的无序集合 对,其中名称是字符串,值是字符串,数字, 布尔值,null,对象或数组.

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

数组是零个或多个值的有序序列.

An array is an ordered sequence of zero or more values.

术语对象"和数组"来自以下约定: JavaScript.

The terms "object" and "array" come from the conventions of JavaScript.

所以我把它读为纯字符串,像布尔值

So I reading it as pure string, number boolean like

"a string"

100

true

都是JSON,

但是我的两个同事认为原始类型只能是对象中的值,

But two of my colleagues think that primitive types can only be value in object,

{ ASTRING : "astring" }是JSON,

如果只有"a string",则不会调用此方法,因为它不是JSON格式,

And if there's only "a string", this is not called, as it is not JSON format,

我认为我和我的同事可能不够专业,无法判断

I think I, and my colleagues may not be professional enough to judge it,

所以我想知道,是纯原始类型的JSON吗?

So I want to know, is pure primitive type JSON?

.

对我来说,另一个想法是,JSON被称为交换数据的便捷方法,但是如果这种格式不支持纯字符串,则

Another idea for me is that , JSON is called a convenient way to exchanging data, but if this format didn't support pure string,

就是说,如果我只想抛出一个字符串,我不能使用JSON来做到这一点吗?

that is, if I just want to throw out a string, I can't use JSON to do it?

,并且必须强制将其更改为{ Message : "a message"}

and have to force it change to { Message : "a message"},

并且不能使用我认为更简单的方法,只需抛出"a message" ...?

and cannot use a way which I think is simpler just throw "a message" ...?

推荐答案

{ "astring" }是无效的JSON,"astring"astring都不有效,因为我们需要键和值,例如{ KEY : VALUE }其中KEY始终是字符串,而VALUE可以是字符串,数字,布尔值或null.

{ "astring" } is not valid JSON and neither is "astring" or astring, as we need both a key and a value, e.g. { KEY : VALUE } where KEY is always a string and VALUE can be a string, number, boolean, or null.

根据规格:

是的,正如规范所说,JSON可以是顶级原始值,而无需 包装它的对象. –安迪·雷

Yes, as the spec says, JSON can be a top level primitive value without an object wrapping it. – Andy Ray

如果我正确理解,则该评论不正确.有效的JSON本身从来都不是顶级原始值.如果您仍然感到困惑,这应该可以解决问题:

If I understood it correctly, that comment is not correct. Valid JSON is never a top-level primitive value by itself. If you're still confused, this should clear things up:

  1. JSON语法

  1. JSON Grammar

JSON文本是令牌序列.令牌集包括 六个结构字符,字符串,数字和三个文字 名称.

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

JSON文本是序列化的对象或数组.

A JSON text is a serialized object or array.

JSON-text = object / array

这些是六个结构特征:

begin-array = ws %x5B ws ; [ left square bracket

begin-object = ws %x7B ws ; { left curly bracket

end-array = ws %x5D ws ; ] right square bracket

end-object = ws %x7D ws ; } right curly bracket

name-separator = ws %x3A ws ; : colon

value-separator = ws %x2C ws ; , comma

六个空格中的任何一个之前或之后都可以使用无关紧要的空格 结构特征.

Insignificant whitespace is allowed before or after any of the six structural characters.

这篇关于是否将原始类型视为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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