$ .each函数不是迭代

时间:2017-06-14 21:01:29

标签: javascript json ajax

我正在使用相对复杂的JSON结构,无法通过$ .each()函数迭代它。我很确定这与我在正常数组的值部分中传递的奇怪的二维数组有关(希望有意义)。我是Ajax和JSON的新手,所以当通过AJAX调用返回时,需要一些关于处理JSON的最佳方法的指导。谢谢!

$.ajax({
        type: 'POST',
        url: 'model.php',
        data: formData,
        dataType: 'json',
         encode : true 
        }).done(function(data){
        
                        var i = 0;
                      
                        for(var k in data){ 
                            window.alert(k);  
                        }   //this works         
            
                      $.each(data, function(key, value){ //however this does not display anything
                         
                          window.alert("should be outputted");
                          window.alert("key" + key + "value" + value[i]['email']);
                          i++;
                          
                      });
                  });

我正在使用的JSON:

{"bodge.com":[{"email":"mgbbuqc@bodge.com","orders":"2","value":"19.67"},{"email":"vswmdkqtb@bodge.com","orders":"5","value":"21.89"},{"email":"fwzqfjma@bodge.com","orders":"3","value":"13.71"},{"email":"rwsofs@bodge.com","orders":"7","value":"6.49"},{"email":"vfr@bodge.com","orders":"3","value":"24.36"},{"email":"wcs@bodge.com","orders":"3","value":"11.26"},{"email":"oqmsboag@bodge.com","orders":"3","value":"5.36"},{"email":"wdvm@bodge.com","orders":"6","value":"18.21"},{"email":"xubumenme@bodge.com","orders":"1","value":"10.24"},{"email":"pleqlwpha@bodge.com","orders":"2","value":"6.59"},{"email":"dqhdcnvw@bodge.com","orders":"10","value":"7.85"},{"email":"gyxymze@bodge.com","orders":"1","value":"15.51"},{"email":"otbbqcw@bodge.com","orders":"2","value":"7.92"},{"email":"afspqpq@bodge.com","orders":"3","value":"13.22"},{"email":"fwovyddw@bodge.com","orders":"4","value":"23.14"},{"email":"urczmgy@bodge.com","orders":"7","value":"15.17"},{"email":"hkgccna@bodge.com","orders":"4","value":"17.62"},{"email":"hlrnunyf@bodge.com","orders":"4","value":"22.03"},{"email":"gafoubu@bodge.com","orders":"10","value":"16.71"},{"email":"muwfjqs@bodge.com","orders":"4","value":"6.09"},{"email":"ddjeqvu@bodge.com","orders":"1","value":"23.88"},{"email":"jbq@bodge.com","orders":"8","value":"5.37"}],"bodge.com ":[{"email":"uytdlcgd@bodge.com ","orders":" 9 ","value":" 21.22"}]}

3 个答案:

答案 0 :(得分:1)

您的JSON(至少)有两个级别:

  1. 包含键("bodge.com", "bodge.com ")和
  2. 的对象
  3. 每个键包含一个对象数组

    {
        "bodge.com": [
            {"email":"mgbbuqc@bodge.com","orders":"2","value":"19.67"},
            {"email":"vswmdkqtb@bodge.com","orders":"5","value":"21.89"},
            ...
            {"email":"ddjeqvu@bodge.com","orders":"1","value":"23.88"},
            {"email":"jbq@bodge.com","orders":"8","value":"5.37"}
        ],
        "bodge.com ": [
            {"email":"uytdlcgd@bodge.com ","orders":" 9 ","value":" 21.22"}
        ]
    }
    
  4. 为了遍历结构,您至少需要两个迭代级别:

    $.each(data, function(domain, objects) {
        console.log(domain); // will output "bodge.com" or "bodge.com "
        $.each(objects, function(index, x) {
            console.log(x.email);
            console.log(x.orders);
            console.log(x.value);
            console.log(index); // will output the 0-based position of x within the array
        });
    });
    

    请注意,您正在使用$.each进行两种不同的迭代:首先是对象的键和值,然后是数组中的项。

    作为替代方案,使用Object.keys获取对象键的数组,以及forEach方法:

    Object.keys(data).forEach(function(domain) {
        console.log(domain); // will output "bodge.com" or "bodge.com "
        data[domain].forEach(function(x, index) {
            console.log(x.email);
            console.log(x.orders);
            console.log(x.value);
            console.log(index); // will output the 0-based position of x within the array
        });
    });
    

答案 1 :(得分:0)

看起来你应该绕过数据['bodge.com']进入每个函数,而不仅仅是数据。

答案 2 :(得分:0)

你可以试试这个;

$.ajax({
    type: 'POST',
    url: 'model.php',
    data: formData,
    dataType: 'json',
     encode : true 
    }).done(function(data){

var list = JSON.parse(data);     

                  $.each(list, function(i){
                     alert(list[i].your_filed);
                  });            
});