javascript中的Json响应没有正确循环

时间:2016-03-31 10:03:30

标签: javascript jquery json

我有一个JsonResponse,如下所示:

    [{"pk": 1, "fields": {"email": "info@spott.com", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "info@spott.com", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}]

我试图访问这样的数据:

    for (var key in data) {
            console.log(key)
            console.log(data[key]);
        }

响应是每个字母而不是每个对象。这有点奇怪,在查看了stackoverflow的答案后,我尝试了其他方法,但总是得到相同的结果。

结果应该是group_name和group_id。任何人都可以帮助我吗?提前谢谢

5 个答案:

答案 0 :(得分:2)

实际上这是一个数组,所以你可以使用经典的for循环简单地循环它,这是一个例子:

    var data = [{"pk": 1, "fields": {"email": "info@spott.com", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
            {"pk": 2, "fields": {"email": "info@spott.com", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

            for (var i=0; i<data.length; i++) {
               console.log(data[i]); //You will get an object
               console.log(data[i].pk);
            }

在每次迭代中,您将获得一个对象,然后您可以访问其属性。

修改

它依赖于此处data的类型,如果它是您在评论中提到的string,则应首先使用JSON.parse(data);解析它然后您可以循环使用它。

否则,如果它是array,您只需要直接遍历其元素。

答案 1 :(得分:1)

尝试使用$ .each而不是for循环:

试试这个:

var object = [{"pk": 1, "fields": {"email": "info@spott.com","locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "info@spott.com", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

$.each(object, function(index, singleObject) {
   console.log("Single Object :%O",singleObject);
});

在控制台中,您将获得正确的对象。

对于小提琴链接点击此处:Fiddle Link

答案 2 :(得分:0)

你去..

<script>
var object =  [{"pk": 1, "fields": {"email": "info@spott.com", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "info@spott.com", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

for(var i=0; i<object.length; i++){
    console.log(object[i].pk);
    console.log(object[i].fields.email);
    console.log(object[i].fields.group_id);
}


</script>

如果不是预期的输出,请告诉我。

答案 3 :(得分:0)

响应采用编码方式,对于您想要解码响应的访问。

var x=[{"pk": 1, "fields": {"email": "info@spott.com", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},{"pk": 2, "fields": {"email": "info@spott.com", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}]; 

var object=JSON.parse(x);//you will get an object then you can find what you want. 

答案 4 :(得分:0)

你可以尝试下面的简单说明:

var array=[{"pk": 1, "fields": {"email": "info@spott.com", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "info@spott.com", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

array.forEach(function(obj){
 //to get the current object 
  console.log(obj)
 // to access to the attribute of the current object 
// example 
console.log(obj.fields.email)

});