Is a primitive type considered JSON?

后端 未结 3 857
粉色の甜心
粉色の甜心 2021-02-14 10:31

Most place the JSON is in format like

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

Or

[  
    { color: \"red\",     value: \"#f0         


        
相关标签:
3条回答
  • 2021-02-14 11:07

    The relevant RFC is RFC 7159, not RFC 4627. RFC 4627 is "informational". RFC 7159 is "standards track"; it explicitly obsoletes 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.

    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
    
    0 讨论(0)
  • 2021-02-14 11:20

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

    From the spec:

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

    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 Grammar

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

      A JSON text is a serialized object or array.

      JSON-text = object / array

      These are the six structural characters:

      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.

    0 讨论(0)
  • 2021-02-14 11:23

    FWIW, here's output from the command line JSON parser 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.

    0 讨论(0)
提交回复
热议问题