Do the JSON keys have to be surrounded by quotes?

后端 未结 6 981
生来不讨喜
生来不讨喜 2020-11-22 11:23

Example: Is the following code valid against the JSON Spec?

{
    precision: \"zip\"
}

Or should I always use the following syntax? (And if

6条回答
  •  梦谈多话
    2020-11-22 12:14

    You are correct to use strings as the key. Here is an excerpt from RFC 4627 - The application/json Media Type for JavaScript Object Notation (JSON)

    2.2. Objects

    An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

    object = begin-object [ member *( value-separator member ) ] end-object

    member = string name-separator value

    [...]

    2.5. Strings

    The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. [...]

    string = quotation-mark *char quotation-mark

    quotation-mark = %x22 ; "

    Read the whole RFC here.

提交回复
热议问题