原始类型是否被视为JSON?

时间:2023-01-16 16:30:10

Most place the JSON is in format like

大多数地方JSON的格式都是这样的

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

Or

要么

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

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

而我想问的是原始类型如string,bool,int也是JSON?

I have found follow link,

我找到了以下链接,

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

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

http://www.json.org/

http://www.json.org/

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

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

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

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

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

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

And,

和,

In RFC4627 it says

在RFC4627中它说

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

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

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

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

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.

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

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

术语“对象”和“数组”来自JavaScript的约定。

So I reading it as pure string, number boolean like

所以我把它读作纯字符串,数字布尔值就好

"a string"

100

true

Are all JSON,

都是JSON,

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

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

{ ASTRING : "astring" } is JSON,

{ASTRING:“astring”}是JSON,

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

如果只有“一个字符串”,则不会调用它,因为它不是JSON格式,

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

我想我,我的同事可能不够专业,无法判断,

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?

也就是说,如果我只是想抛弃一个字符串,我不能使用JSON来做它?

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

并且必须强制它改为{消息:“消息”},

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

并且不能使用我认为更简单的方式只是抛出“消息”......?

3 个解决方案

#1


3  

{ "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.

{“astring”}不是有效的JSON,也不是“astring”或astring,因为我们需要一个键和一个值,例如{KEY:VALUE}其中KEY始终是一个字符串,VALUE可以是字符串,数字,布尔值或null。

From the spec:

从规格:

原始类型是否被视为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:

如果我理解正确,该评论是不正确的。有效的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文本是序列化对象或数组。

    JSON-text = object / array

    JSON-text =对象/数组

    These are the six structural characters:

    这些是六个结构特征:

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

    begin-array = ws%x5B ws; [左方括号

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

    begin-object = ws%x7B ws; {左大括号

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

    end-array = ws%x5D ws; ]右方括号

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

    end-object = ws%x7D ws;右大括号

    name-separator = ws %x3A ws ; : colon

    name-separator = ws%x3A ws; :冒号

    value-separator = ws %x2C ws ; , comma

    value-separator = ws%x2C ws; ,逗号

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

    在六个结构字符中的任何一个之前或之后允许无关紧要的空白。

#2


1  

FWIW, here's output from the command line JSON parser prgram jq:

FWIW,这是命令行JSON解析器prgram jq的输出:

$ echo "{ foo }" | jq .
parse error: Invalid literal at line 1, column 6

$ echo "{ \"foo\" }" | jq .
parse error: Objects must consist of key:value pairs at line 1, column 9

$ echo "\"foo\"" | jq .
"foo"

I don't have access to other parser at the moment.

我目前无法访问其他解析器。

#3


1  

The relevant RFC is RFC 7159, not RFC 4627. RFC 4627 is "informational". RFC 7159 is "standards track"; it explicitly obsoletes RFC 4627.

相关的RFC是RFC 7159,而不是RFC 4627.RFC 4627是“信息性的”。 RFC 7159是“标准跟踪”;它明确废弃了RFC 4627。

Request for Comments: 7159                                  Google, Inc.
Obsoletes: 4627, 7158                                         March 2014
Category: Standards Track
ISSN: 2070-1721

In the text of RFC 7159, you'll find this.

在RFC 7159的文本中,您会发现这一点。

13.  Examples

   This is a JSON object:

      {
        "Image": {
            "Width":  800,
            "Height": 600,
            "Title":  "View from 15th Floor",
            "Thumbnail": {
                "Url":    "http://www.example.com/image/481989943",
                "Height": 125,
                "Width":  100
            },
            "Animated" : false,
            "IDs": [116, 943, 234, 38793]
          }
      }

[snip]

[剪断]

   Here are three small JSON texts containing only values:

   "Hello world!"

   42

   true

#1


3  

{ "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.

{“astring”}不是有效的JSON,也不是“astring”或astring,因为我们需要一个键和一个值,例如{KEY:VALUE}其中KEY始终是一个字符串,VALUE可以是字符串,数字,布尔值或null。

From the spec:

从规格:

原始类型是否被视为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:

如果我理解正确,该评论是不正确的。有效的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文本是序列化对象或数组。

    JSON-text = object / array

    JSON-text =对象/数组

    These are the six structural characters:

    这些是六个结构特征:

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

    begin-array = ws%x5B ws; [左方括号

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

    begin-object = ws%x7B ws; {左大括号

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

    end-array = ws%x5D ws; ]右方括号

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

    end-object = ws%x7D ws;右大括号

    name-separator = ws %x3A ws ; : colon

    name-separator = ws%x3A ws; :冒号

    value-separator = ws %x2C ws ; , comma

    value-separator = ws%x2C ws; ,逗号

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

    在六个结构字符中的任何一个之前或之后允许无关紧要的空白。

#2


1  

FWIW, here's output from the command line JSON parser prgram jq:

FWIW,这是命令行JSON解析器prgram jq的输出:

$ echo "{ foo }" | jq .
parse error: Invalid literal at line 1, column 6

$ echo "{ \"foo\" }" | jq .
parse error: Objects must consist of key:value pairs at line 1, column 9

$ echo "\"foo\"" | jq .
"foo"

I don't have access to other parser at the moment.

我目前无法访问其他解析器。

#3


1  

The relevant RFC is RFC 7159, not RFC 4627. RFC 4627 is "informational". RFC 7159 is "standards track"; it explicitly obsoletes RFC 4627.

相关的RFC是RFC 7159,而不是RFC 4627.RFC 4627是“信息性的”。 RFC 7159是“标准跟踪”;它明确废弃了RFC 4627。

Request for Comments: 7159                                  Google, Inc.
Obsoletes: 4627, 7158                                         March 2014
Category: Standards Track
ISSN: 2070-1721

In the text of RFC 7159, you'll find this.

在RFC 7159的文本中,您会发现这一点。

13.  Examples

   This is a JSON object:

      {
        "Image": {
            "Width":  800,
            "Height": 600,
            "Title":  "View from 15th Floor",
            "Thumbnail": {
                "Url":    "http://www.example.com/image/481989943",
                "Height": 125,
                "Width":  100
            },
            "Animated" : false,
            "IDs": [116, 943, 234, 38793]
          }
      }

[snip]

[剪断]

   Here are three small JSON texts containing only values:

   "Hello world!"

   42

   true