admin管理员组文章数量:1202350
I keep getting this error message when I load my human.json file via AJAX.
The whole error message reads
JSON.parse: expected ',' or '}' after property value
in object at line 2 column 22 of the JSON data.
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?
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();
and my human.json file:
{
"sex":{
"male":{"fname":["Michael", "Tom"]},
"female"
},
"age":[16, 80],
"job":[]
}
I keep getting this error message when I load my human.json file via AJAX.
The whole error message reads
JSON.parse: expected ',' or '}' after property value
in object at line 2 column 22 of the JSON data.
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?
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();
and my human.json file:
{
"sex":{
"male":{"fname":["Michael", "Tom"]},
"female"
},
"age":[16, 80],
"job":[]
}
Share
Improve this question
edited May 4, 2016 at 5:03
Hawken MacKay Rives
1,2091 gold badge17 silver badges26 bronze badges
asked May 4, 2016 at 3:13
black_yurizanblack_yurizan
4372 gold badges7 silver badges20 bronze badges
2
- 4 Your object doesn't make sense. – SLaks Commented May 4, 2016 at 3:18
- Possible duplicate of JSON.parse array nested element not working – evolutionxbox Commented May 4, 2016 at 16:50
5 Answers
Reset to default 8Your object isn't valid JSON. Specifically at the part:
,"female"}
A JSON property must have a value. Maybe that should that be:
,"female":{}}
or:
,"female":null}
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":[]
}
In JSON, objects have the syntax:
{"name" : "value"}
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":[]
}
Your JSON is indeed invalid.
{
"sex": {
"male":{
"fname": ["Michael","Tom"]
},
"female" ## Here is the problem
},
"age": [16,80],
"job": []
}
Perhaps change that line to:
"female": {}
It all depends on what you're wanting to do
your "female"
is error, need a key or value
you can change the json file to
{
"sex":{"male":{"fname":["Michael","Tom"]} ,"female":null},
"age":[16,80],
"job":[]
}
JSON file used by you is invalid json. JSON is a collection of name/value pair. Each key in the JSON should contain value. In your case, key "Female" doesn't have any value. Below shown is the valid JSON format.
{
"sex": {
"male": {
"fname": ["Michael", "Tom"]
},
"female": "XXX"
},
"age": [16, 80],
"job": []
}
本文标签: javascriptJSONparse expected 3939 or 3939 after property value in objectStack Overflow
版权声明:本文标题:javascript - JSON.parse: expected ',' or '}' after property value in object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738649839a2104806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论