Is a value on its own a valid JSON string?

In a JSON API, is it valid to return single values such as 123 , "somestring" or null ?

I read the JSON spec, which states that:

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
  • but I'm not clear if this means that only objects and arrays are valid JSON, or if values on their own are valid too (ie will be parsed correctly by any compliant parser).

    Any idea?


    No, it's not valid. Check this out if you want to experiment with anything.

    The two structures are as follows:

    Some kind of key-value pair:

    {
      "key": "value"
    }
    

    or an array

    ['value', 'value']
    

    or any combination of the two

    [{"key":"value"}, "value", ["a", "list", {"another":['list']}]]
    

    However, the values on their own (numbers, strings, booleans, etc., are not valid on their own.


    Is 123 a collection of name/value pairs? No, it is not.
    Is 123 an ordered list of values? No, it is not.

    Thus, 123 is not a valid JSON string.

    Edit: As gdoron suggested, you could use a JSON parser (eg here) to test your single value.

    链接地址: http://www.djcxy.com/p/37874.html

    上一篇: 使用命令行参数从C#执行PowerShell脚本

    下一篇: 它自己的值是一个有效的JSON字符串吗?