JQuery JSON解析/ AJAX问题(数组中的'remove'和'__proto__'键?)

tjm*_*hta 2 javascript jquery parsing json

当使用Jquery $ .ajax函数从URL获取JSON对象时,我发现了一个奇怪的问题.

我使用以下JQuery调用来检索JSON对象:

1  $.ajax({
2       url: '/test/getjson',
3       success: function(data){
4                     doSomething(data); //(breakpoint here)
5                },
6       error: function(x,y,z){
7                     //error..
8               }
9  });
Run Code Online (Sandbox Code Playgroud)

URL'/ test/getjson'返回以下JSON对象(以下是Firebug中的响应):

{
    "rsp": {
        "date": "1299195954782" ,
        "type": "Type1" ,
        "Main": {
            "Category1" : {
                "private" : "Y" ,
                "properties" : {
                    "one" : {
                        "response" : "" ,
                        "text" : "label" ,
                        "type" : "property"
                    },
                    "two" : {
                        "options" : [
                            "1" ,
                            "2" ,
                            "3" ,
                            "4" ,
                            "5" ,
                            "6" ,
                            "7" ,
                            "8" ,
                            "9" ,
                            "10"
                        ],
                        "response" : "1" ,
                        "text" : "label2" ,
                        "type" : "property2"
                    }
                }
            },
            "username" : "spiderman"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题

问题是JSON对象中的所有数组在被JQuery解析后都有额外的值'remove'和'__proto__'(Firebug的调试器查看JSON数据对象,上面JS代码段第4行的断点):

在此输入图像描述

[Firebugs调试器看到的JSON对象]

这里仔细看看JSON对象的奇怪部分:

在此输入图像描述

[仔细查看未知数据]

谢谢大家:)

Poi*_*nty 8

停止使用"for ... in"迭代数组,并使用索引变量.

for (var i = 0; i < theArray.length; ++i) {
  var element = theArray[i];
  // ...
}
Run Code Online (Sandbox Code Playgroud)

由于浏览器中的JavaScript运行时,这些对象属性就在那里.我不确定为什么他们会给你带来任何麻烦,因为它们不应该是可迭代的.也许如果你展示了你必须处理ajax响应的实际代码,问题可能会变得更加清晰.