繁体   English   中英

带有Ajax响应字符串的JSON解析错误

[英]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如下:

固定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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM