Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的Json响应循环不正确_Javascript_Jquery_Json - Fatal编程技术网

javascript中的Json响应循环不正确

javascript中的Json响应循环不正确,javascript,jquery,json,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": "spottal

我有一个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]);
        }
我尝试过这样访问数据:

    [{"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的答案后,我也尝试了其他方法,但总是得到相同的结果

结果应该是组名和组id。有人能帮我吗?提前谢谢你

给你

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

var对象=[{“pk”:1,“字段”:{“电子邮件”:”info@spott.com“,“位置”:[1],“组id”:“spott”,“组名称”:“spott”},“模型”:“分组”},
{“pk”:2,“字段”:{“电子邮件”:”info@spott.com“,“位置”:[1,2],“组id”:“spottalle”,“组名称”:“spottalle”},“模型”:“grouping”}];

for(var i=0;i事实上这是一个数组,因此您可以简单地使用经典for循环对其进行循环,这是一个exmaple代码段:

    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);
            }
var data=[{“pk”:1,“字段”:{“email”:”info@spott.com“,“位置”:[1],“组id”:“spott”,“组名称”:“spott”},“模型”:“分组”},
{“pk”:2,“字段”:{“电子邮件”:”info@spott.com“,“位置”:[1,2],“组id”:“spottalle”,“组名称”:“spottalle”},“模型”:“grouping”}];

对于(var i=0;i响应是以编码方式进行的,对于要解码响应的访问

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. 

您可以尝试以下简单说明:

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)

});

尝试使用$。每个而不是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链接,请单击此处:

这是一个数组…使用
For loop…
而不是
For in.
确保您正在迭代
JSON
…您必须拥有
对象/数组
@RayonDabre您的意思是这样的吗?For(var i=0;i对象/数组
…检查
数据类型
@RayonDabre typof data说它是字符串:否则你必须在循环之前解析它。
JSON.parse(数据);
它是
JSON
不是
Array
。问题是回答是每一个字母,不是每一个object。是的,你是对的,但它可以使用for循环。从给定的输入(JSON)对于OP,当你循环时,你会得到对象,你可以测试我的代码片段,你会看到它在工作。浏览注释,你就会知道。你必须从响应中找出它是
JSON
,不是每个字母都是object是的,你确实是对的,因为它是OP中的一个字符串,例如,我使用了给定的输入如您所见的数组。