在json中使用方括号的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36688321/
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 18:23:31  来源:igfitidea点击:

what is the purpose of using square brackets in json?

jsonsquare-bracket

提问by Uswer721

I am new to json. Some json examples i have seen have data within the curly braces and some json examples have subdata within square brackets.

我是 json 的新手。我见过的一些 json 示例在花括号中包含数据,而一些 json 示例在方括号中包含子数据。

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}
}

From http://json.org/example.html

来自http://json.org/example.html

What is the need/purpose of having data within the square brackets?

将数据放在方括号内的需要/目的是什么?

regards

问候

回答by Boy

The square brackets produce a list/array.

方括号产生一个列表/数组。

The curly brackets produce an object with key/value pairs.

大括号产生一个带有键/值对的对象。

The list can then be a value of a key/value pair.

该列表然后可以是键/值对的值。

回答by Samuel LEMAITRE

[]means an array of object (a list) and {}means it will be an object.

[]表示一个对象数组(一个列表),{}表示它将是一个对象。

Example:

例子:

{
    "ID":"test",
    "sports": [
        "volley-ball",
        "badminton"
    ]
}

To get the ID, you can do: myjsonobject.ID(here you will get "test")

要获取 ID,您可以执行以下操作myjsonobject.ID:(在这里您将获得“测试”)

And for sports: myjsonobject.sports[0](here you will get "volley-ball")

对于运动:(myjsonobject.sports[0]在这里你会得到“排球”)