How do you represent a JSON array of strings?

后端 未结 4 1139
野趣味
野趣味 2020-12-04 05:12

This is all you need for valid JSON, right?

[\"somestring1\", \"somestring2\"]
相关标签:
4条回答
  • 2020-12-04 05:55

    Basically yes, JSON is just a javascript literal representation of your value so what you said is correct.

    You can find a pretty clear and good explanation of JSON notation on http://json.org/

    0 讨论(0)
  • 2020-12-04 06:00

    I'll elaborate a bit more on ChrisR awesome answer and bring images from his awesome reference.

    A valid JSON always starts with either curly braces { or square brackets [, nothing else.

    { will start an object:

    left brace followed by a key string (a name that can't be repeated, in quotes), colon and a value (valid types shown below), followed by an optional comma to add more pairs of string and value at will and finished with a right brace

    { "key": value, "another key": value }
    

    Hint: although javascript accepts single quotes ', JSON only takes double ones ".

    [ will start an array:

    left bracket followed by value, optional comma to add more value at will and finished with a right bracket

    [value, value]
    

    Hint: spaces among elements are always ignored by any JSON parser.

    And value is an object, array, string, number, bool or null:

    Image showing the 6 types a JSON value can be: string, number, JSON object, Array/list, boolean, and null

    So yeah, ["a", "b"] is a perfectly valid JSON, like you could try on the link Manish pointed.

    Here are a few extra valid JSON examples, one per block:

    {}
    
    [0]
    
    {"__comment": "json doesn't accept comments and you should not be commenting even in this way", "avoid!": "also, never add more than one key per line, like this"}
    
    [{   "why":null} ]
    
    {
      "not true": [0, false],
      "true": true,
      "not null": [0, 1, false, true, {
        "obj": null
      }, "a string"]
    }
    
    0 讨论(0)
  • 2020-12-04 06:08

    Your JSON object in this case is a list. JSON is almost always an object with attributes; a set of one or more key:value pairs, so you most likely see a dictionary:

    { "MyStringArray" : ["somestring1", "somestring2"] }
    

    then you can ask for the value of "MyStringArray" and you would get back a list of two strings, "somestring1" and "somestring2".

    0 讨论(0)
  • 2020-12-04 06:10
    String strJson="{\"Employee\":
    [{\"id\":\"101\",\"name\":\"Pushkar\",\"salary\":\"5000\"},
    {\"id\":\"102\",\"name\":\"Rahul\",\"salary\":\"4000\"},
    {\"id\":\"103\",\"name\":\"tanveer\",\"salary\":\"56678\"}]}";
    

    This is an example of a JSON string with Employee as object, then multiple strings and values in an array as a reference to @cregox...

    A bit complicated but can explain a lot in a single JSON string.

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