JSON解析错误:期待'STRING'

yit*_*zih 9 json

我使用JSONLint来解析一些JSON,我不断收到错误:

错误:第1行的解析错误:[{"product":[{"
--- ^期待'STRING','}',得到'undefined'

这是代码:

[
    {
        “product” :  [ { “code” : “Abc123”, “description” : “Saw blade”, “price” : 34.95 } ],
        “vendor” : [ { “name” : “Acme Hardware”, “state” : “New Jersey” } ]
    },

    {
        “product” :  [ { “code” : “Def456”, “description” : “Hammer”, “price” : 22.51 } ],
    },

    {
        “product” :  [ { “code” : “Ghi789”, “description” : “Wrench”, “price” : 12.15 } ],
        “vendor” : [ { “name” : “Acme Hardware”, “state” : “New Jersey” } ]
    },

    {
        “product” :  [ { “code” : “Jkl012”, “description” : “Pliers”, “price” : 14.54 } ],
        “vendor” : [ { “name” : “Norwegian Tool Suppliers”, “state” : “Kentucky” } ]
    }
]   
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 22

JSON字符串文字必须使用普通引号字符("),而不是智能引号(“”).

  • 值得注意的是,即使转换为普通引号字符,JSON仍然无效.在"价格"之后有一个尾随的逗号:22.51}],`.后面没有任何项目,因此应删除逗号. (3认同)
  • 我刚用单引号犯了这个错误,所以不要使用那些:) (2认同)

11t*_*ion 7

您正在使用一些 unicode 双引号字符。将它们替换为普通的"双引号。

在第二个元素的末尾还有一些额外的逗号。

现在好了

[
    {
        "product" :  [ { "code" : "Abc123", "description" : "Saw blade", "price" : 34.95 } ],
        "vendor" : [ { "name" : "Acme Hardware", "state" : "New Jersey" } ]
    },

    {
        "product" :  [ { "code" : "Def456", "description" : "Hammer", "price" : 22.51 } ]
    },
    {
        "product" :  [ { "code" : "Ghi789", "description" : "Wrench", "price" : 12.15 } ],
        "vendor" : [ { "name" : "Acme Hardware", "state" : "New Jersey" } ]
    },
    {
        "product" : [ { "code" : "Jkl012", "description" : "Pliers", "price" : 14.54 } ],
        "vendor" : [ { "name" : "Norwegian Tool Suppliers", "state" : "Kentucky" } ]
    }
]
Run Code Online (Sandbox Code Playgroud)