what is the purpose of using square brackets in json? [duplicate]

痴心易碎 提交于 2019-12-04 08:03:24

问题


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.

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

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

regards


回答1:


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.




回答2:


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

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



来源:https://stackoverflow.com/questions/36688321/what-is-the-purpose-of-using-square-brackets-in-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!