JSON格式的问题

时间:2015-10-10 00:42:43

标签: javascript json

Chrome一直告诉我JSON的语法错误,我不明白为什么。我想要一个“游戏”数组,并且每个对象都是一个不同的游戏,所有值都在JSON文件中进行硬编码。它说我的第一个对象很好,但一旦它到达第二个对象打开花括号,它告诉我语法不正确。我按照我之前在这个网站上找到的一个例子进行了验证,因此不确定为什么我的是轰炸。

var games = [
    {
        "title" : "Mass Effect",
        "genre" : "Third-Person Shooter",
        "date" : "March 6, 2012",
        "character" : "Shepard",
        "weapon" : [
            {
             "Pistol" : "Kessler",
             "Assault Rifle" : "Lancer",
             "Shotgun" : "Sokolov",
             "Sniper Rifle" : "Titan"
            }
        ],  
    }
    { //this bracket here is where it is telling me my syntax is wrong
        "title" : "Resident Evil",
        "genre" : "Survival Horror",
        "date" : "March 22, 1996",
        "character" : "Chris Redfield",
        "weapon" : [
            {
             "Handgun" : "Nine-Oh-Nine",
             "Assault Rifle" : "Bear Commander",
             "Shotgun" : "Triple Shot",
             "Rifle" : "Anti-Matter Rifle"
            }
        ],
    }
    {
        "title" : "Halo",
        "genre" : "First-Person Shooter",
        "date" : "November 15, 2001",
        "character" : " Master Chief",
        "weapon" : [
            {
             "Pistol" : "M6D Personal Defense Weapon",
             "Assault Rifle" : "MA5B Individual Combat Weapon",
             "Shotgun" : "Oathsworn",
             "Rifle" : "BR85 Heavy Barrel Service Rifle"
            }
        ],
    }
    {
        "title" : "Final Fantasy 7",
        "genre" : "Role-Playing Game",
        "date" : "January 31, 1997",
        "character" : "Cloud Strife",
        "weapon" :  [
            {
             "Two-Handed Sword" : "Buster Sword",
             "Katana" : "Murasame",
             "Blunt Weapon" : "Nail Bar",
             "Ultimate Weapon" : "Ultima Weapon"
            }
        ]
    }
]

2 个答案:

答案 0 :(得分:1)

每个“游戏”应以逗号分隔。

尝试 -

var games = [
{
    "title" : "Mass Effect",
    "genre" : "Third-Person Shooter",
    "date" : "March 6, 2012",
    "character" : "Shepard",
    "weapon" : [
        {
         "Pistol" : "Kessler",
         "Assault Rifle" : "Lancer",
         "Shotgun" : "Sokolov",
         "Sniper Rifle" : "Titan"
        }
    ],  
},
{ //this bracket here is where it is telling me my syntax is wrong
    "title" : "Resident Evil",
    "genre" : "Survival Horror",
    "date" : "March 22, 1996",
    "character" : "Chris Redfield",
    "weapon" : [
        {
         "Handgun" : "Nine-Oh-Nine",
         "Assault Rifle" : "Bear Commander",
         "Shotgun" : "Triple Shot",
         "Rifle" : "Anti-Matter Rifle"
        }
    ],
},
{
    "title" : "Halo",
    "genre" : "First-Person Shooter",
    "date" : "November 15, 2001",
    "character" : " Master Chief",
    "weapon" : [
        {
         "Pistol" : "M6D Personal Defense Weapon",
         "Assault Rifle" : "MA5B Individual Combat Weapon",
         "Shotgun" : "Oathsworn",
         "Rifle" : "BR85 Heavy Barrel Service Rifle"
        }
    ],
},
{
    "title" : "Final Fantasy 7",
    "genre" : "Role-Playing Game",
    "date" : "January 31, 1997",
    "character" : "Cloud Strife",
    "weapon" :  [
        {
         "Two-Handed Sword" : "Buster Sword",
         "Katana" : "Murasame",
         "Blunt Weapon" : "Nail Bar",
         "Ultimate Weapon" : "Ultima Weapon"
        }
    ]
},
...etc
];

答案 1 :(得分:1)

尝试以下代码,

每个应分别用逗号

分隔
[
    {
        "title": "Mass Effect",
        "genre": "Third-Person Shooter",
        "date": "March 6, 2012",
        "character": "Shepard",
        "weapon": [
            {
                "Pistol": "Kessler",
                "Assault Rifle": "Lancer",
                "Shotgun": "Sokolov",
                "Sniper Rifle": "Titan"
            }
        ]
    },
    {
        "title": "Resident Evil",
        "genre": "Survival Horror",
        "date": "March 22, 1996",
        "character": "Chris Redfield",
        "weapon": [
            {
                "Handgun": "Nine-Oh-Nine",
                "Assault Rifle": "Bear Commander",
                "Shotgun": "Triple Shot",
                "Rifle": "Anti-Matter Rifle"
            }
        ]
    },
    {
        "title": "Halo",
        "genre": "First-Person Shooter",
        "date": "November 15, 2001",
        "character": " Master Chief",
        "weapon": [
            {
                "Pistol": "M6D Personal Defense Weapon",
                "Assault Rifle": "MA5B Individual Combat Weapon",
                "Shotgun": "Oathsworn",
                "Rifle": "BR85 Heavy Barrel Service Rifle"
            }
        ]
    },
    {
        "title": "Final Fantasy 7",
        "genre": "Role-Playing Game",
        "date": "January 31, 1997",
        "character": "Cloud Strife",
        "weapon": [
            {
                "Two-Handed Sword": "Buster Sword",
                "Katana": "Murasame",
                "Blunt Weapon": "Nail Bar",
                "Ultimate Weapon": "Ultima Weapon"
            }
        ]
    }
]