JSON.parse:对象的属性值后应为','或'}' [英] JSON.parse: expected ',' or '}' after property value in object

查看:117
本文介绍了JSON.parse:对象的属性值后应为','或'}'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过AJAX加载human.json文件时,我不断收到此错误消息.

I keep getting this error message when I load my human.json file via AJAX.

整个错误消息显示为

JSON.parse: expected ',' or '}' after property value 
in object at line 2 column 22 of the JSON data. 

我在网上寻找它,有些人也收到类似的错误消息,但是他们没有通过AJAX打电话.

I looked online for it, and there have been people who had similar error messages, however, they are not calling via AJAX.

除此之外,它们不是在对象内的对象内嵌套数组.我认为这就是我收到此错误消息的原因.您是否不允许彼此嵌套那么多属性?

In addition to that, they are not nesting arrays within objects within objects. I think that is the reason why I am getting this error message. Are you not allowed to nest that many properties into each other?

这是我的AJAX代码:

Here's my AJAX code:

var request = new XMLHttpRequest();

request.open('GET','human.json');

request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {                     
        var obj = JSON.parse(request.responseText);
        console.log(obj);
    }
}

request.send();

和我的human.json文件:

and my human.json file:

{
  "sex":{
    "male":{"fname":["Michael", "Tom"]}, 
    "female"
  },
  "age":[16, 80],
  "job":[]
}

推荐答案

您的JSON文件存在语法错误.重新格式化以下内容以突出显示该错误:

Your JSON file has a syntax error. The following is reformatted to highlight the error:

{
 "sex":{
  "male":{"fname":["Michael","Tom"]},
  "female"        <----------------- SYNTAX ERROR
 },
 "age":[16,80],
 "job":[]
}

在JSON中,对象具有以下语法:

In JSON, objects have the syntax:

{"name" : "value"}

根据JSON规范,语法{"foo"}无效.因此,您需要为female属性提供一些值:

The syntax {"foo"} is invalid according to JSON spec. Therefore you need to provide some value for the female attribute:

{
 "sex":{
  "male":{"fname":["Michael","Tom"]},
  "female":{}
 },
 "age":[16,80],
 "job":[]
}

这篇关于JSON.parse:对象的属性值后应为','或'}'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆