[英]JSON parse error with ajax response string
我一直在努力解決這個問題,並且已經在Web和stackoverflow中搜索了解決方案,但是我無法確切地得到什么錯誤。
這是通過ajax調用來自服務器的json字符串。
{root:{name: "root",description: "root description",checked: false,1:{name: "item1",description: "item1 description",checked: true,1.1:{name: "item1.1",description: "item1.1 description",checked: true}}, 2:{name: "item2",description: "item2 description",checked: true}}}
使用下面的代碼我得到xmlhttp.readyState == 4 && xmlhttp.status == 200
之后的字符串
var aData;
try{
aData =JSON.parse(xmlhttp.responseText);
}
catch(err){
alert(err);
}
它顯示錯誤
Json.parse expected property name or '}'
但是如果使用eval()函數可以正常工作
var aData;
try{
aData =eval('(' + xmlhttp.responseText + ')');
}
catch(err){
alert(err);
}
請exaplin這里是什么錯誤。
謝謝。
編輯:
我已經在json查看器中檢查了字符串,並且效果很好。 http://jsonviewer.stack.hu/“>
那是因為那是無效的JSON,您不能使用數字作為鍵名。 因此,您可以跳過數字或將其放在引號中,以便它們成為“字符串”。
驗證器輸出和固定的JSON如下:
固定JSON:
{
"root":{
"name":"root",
"description":"root description",
"checked":false,
"1":{
"name":"item1",
"description":"item1 description",
"checked":true,
"1.1":{
"name":"item1.1",
"description":"item1.1 description",
"checked":true
}
},
"2":{
"name":"item2",
"description":"item2 description",
"checked":true
}
}
}
驗證器輸出:
Error:Strings should be wrapped in double quotes.[Code 17, Structure 2]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 5]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 9]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 13]
Error:Expecting string, not number.[Code 8, Structure 17]
Error:Expecting comma or }, not colon.[Code 13, Structure 18]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 20]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 24]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 28]
Error:Expecting string, not number.[Code 8, Structure 32]
Error:Expecting comma or }, not colon.[Code 13, Structure 33]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 35]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 39]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 43]
Error:Expecting string, not number.[Code 8, Structure 49]
Error:Expecting comma or }, not colon.[Code 13, Structure 50]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 52]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 56]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 60]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.