你如何表示一个 JSON 字符串数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5293555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:47:58  来源:igfitidea点击:

How do you represent a JSON array of strings?

json

提问by finneycanhelp

This is all you need for valid JSON, right?

这就是有效 JSON 所需的全部内容,对吗?

["somestring1", "somestring2"]

回答by cregox

I'll elaborate a bit more on ChrisRawesome answerand bring images from his awesome reference.

我将详细说明ChrisR很棒的答案,并从他很棒的参考中带来图像。

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

一个有效的 JSON 总是以花括号{或方括号开头[,没有别的。

{will start an object:

{将开始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 ".

提示:虽然 javascript 接受单引号',但 JSON 只接受双引号"

[will start an array:

[将开始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.

提示:任何 JSON 解析器总是忽略元素之间的空格。

And valueis an object, array, string, number, boolor null:

并且valueobject, array, string, number,boolnull

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

显示 JSON 值可以是 6 种类型的图像:字符串、数字、JSON 对象、数组/列表、布尔值和 null

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

所以是的,["a", "b"]是一个完全有效的 JSON,就像您可以尝试使用 Manish 指出的链接

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

以下是一些额外的有效 JSON 示例,每个块一个:

{}

[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"]
}

回答by PapaSmurf

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:

在这种情况下,您的 JSON 对象是一个列表。JSON 几乎总是一个有属性的对象;一组一个或多个键:值对,所以你很可能会看到一个字典:

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

然后你可以询问 的值,"MyStringArray"你会得到一个包含两个字符串的列表,"somestring1""somestring2"

回答by ChrisR

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

基本上是的,JSON 只是您价值的 javascript 文字表示,所以您说的是正确的。

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

您可以在http://json.org/上找到对 JSON 符号的非常清晰和很好的解释

回答by Pushkar Shrivastava

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 Employeeas object, then multiple strings and values in an array as a reference to @cregox...

这是一个带有Employeeas 对象的 JSON 字符串示例,然后数组中的多个字符串和值作为对@cregox...

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

有点复杂,但可以在单个 JSON 字符串中解释很多。