简体   繁体   English

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

[英]JSON parse error with ajax response string

I have been working on this issue and i have searched for the solution in the web and stackoverflow, but I cant get whats the error exactly. 我一直在努力解决这个问题,并且已经在Web和stackoverflow中搜索了解决方案,但是我无法确切地得到什么错误。

this is the json string coming from the server through the ajax call. 这是通过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}}}

using the below code i am getting the string after xmlhttp.readyState == 4 && xmlhttp.status == 200 使用下面的代码我得到xmlhttp.readyState == 4 && xmlhttp.status == 200之后的字符串

var aData;
        try{
        aData =JSON.parse(xmlhttp.responseText);
        }
        catch(err){
        alert(err);
        }

it shows the error like 它显示错误

Json.parse expected property name or '}'

but if use eval() function it works fine 但是如果使用eval()函数可以正常工作

var aData;
        try{
        aData =eval('(' + xmlhttp.responseText + ')');
        }
        catch(err){
        alert(err);
        }

please exaplin what is the error here. 请exaplin这里是什么错误。

Thanks. 谢谢。

EDIT: 编辑:

I have checked the string in json viewer and its worked fine. 我已经在json查看器中检查了字符串,并且效果很好。 http://jsonviewer.stack.hu/"> http://jsonviewer.stack.hu/“> JSON视图

That's because that's invalid JSON, you cannot use numbers as key names. 那是因为那是无效的JSON,您不能使用数字作为键名。 So, you can either skip the numbers or put them in quotes so that they are "strings". 因此,您可以跳过数字或将其放在引号中,以便它们成为“字符串”。

Validator output and fixed JSON are below: 验证器输出和固定的JSON如下:

Fixed 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
      }
   }
}

Validator output: 验证器输出:

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