【问题标题】:Issues with JSON formatJSON格式的问题
【发布时间】:2015-10-10 00:42:42
【问题描述】:

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

【问题讨论】:

  • 实际错误是什么?没关系,少了一个逗号!将, 放在括号内
  • 你错过了游戏之间的逗号 - 如 - " }, { //这里的括号" - 看到逗号是必要的

标签: javascript json


【解决方案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"
            }
        ]
    }
]

【讨论】:

  • 天哪,好的,谢谢。像这样的小事就溜过去了
【解决方案2】:

每个“游戏”都应该用逗号分隔。

试试 -

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
];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    相关资源
    最近更新 更多