What's the difference between Javascript Object and JSON object

前端 未结 5 1487
南笙
南笙 2020-11-27 02:55

Can anyone tell me the difference between Javascript Object and JSON object with an example?

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 03:18

    JSON stands for "JavaScript Object Notation". Basically, JSON is Javascript, but limited to just filling an object with data. By executing a JSON object, you "load" the data in memory.
    JavaScript is the bigger picture, with additional lines of code to manipulate the object or to do all kinds of other stuff.

    A JSON example would be this:

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

    A JavaScript example would be this:

    var Glossary = {
        "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"
                    }
                }
            }
        }
    }
    

    Notice the var Glossary = in the JavaScript?

提交回复
热议问题