2017-06-22 109 views
1

我有一个包含一个数组的json响应,在该数组内我有一个更多的数组。如何在foreach循环内获取jQuery的foreach值

我的AJAX:

$.ajax({ 
    type: "POST", 
    data: { city: city, priceRange: range }, 
    url: "rentpriceRangeFilter.php", 
    success: function(data) { 
    var res = jQuery.parseJSON(data); // convert the json 
    console.log(res); 
    if (res['status'] == 1) { 
     var htmlString = ''; 
     $.each(res['data'], function(key, value) { 
     console.log(key + ' is ' + value.house); 
     }); 
    } 
    }, 
}); 

rentpriceRangeFilter.php(我返回JSON响应)

echo json_encode(array("status"=>"1","response"=>"success","data"=>$mainArray)); 

我的JSON响应这样的:

{ 
    "status": "1", 
    "response": "success", 
    "data": [{ 
    "id": "2", 
    "house": "1 BHK Apartment for Lease", 
    "rentLease_amount": "6000000", 
    "furnished_type": "Semi-Furnished", 
    "CreatedOn": null, 
    "Rent": "60 Lac", 
    "multipleImages": [{ 
     "imageId": "3", 
     "rentImageId": "2", 
     "rentImageName": "2cf011438e6cd43c6bfaa6cac653d86e.jpg" 
    }, { 
     "imageId": "4", 
     "rentImageId": "2", 
     "rentImageName": "e2c5be420130370c5118120a1bc749c6.jpg" 
    }] 
    }, { 
    "id": "1", 
    "house": "1 BHK Aparatment for Marathahalli", 
    "rentLease_amount": "500000", 
    "furnished_type": "Fully-Furnished", 
    "CreatedOn": null, 
    "Rent": "5 Lac", 
    "multipleImages": [{ 
     "imageId": "1", 
     "rentImageId": "1", 
     "rentImageName": "7d2905c30ab211732b97dbf165c75526.jpg" 
    }, { 
     "imageId": "2", 
     "rentImageId": "1", 
     "rentImageName": "bcf6cb343aaa2cc8e50ff52baa062bcc.jpg" 
    }] 
    }] 
} 

现在我想显示house名称和multipleImages值,但在我的ajax页面,我只能得到房子名称,我不怎么显示multipleImages

回答

0

您可以使用multipleImages另一个循环象下面这样:

$.ajax({ 
    type: "POST", 
    data: {city:city,priceRange:range}, 
    url: "rentpriceRangeFilter.php", 
    success:function(data){ 
     var res=jQuery.parseJSON(data);// convert the json 
     console.log(res); 

     if(res['status']==1){ 
      var htmlString=''; 
      $.each(res['data'], function(key, value) { 
       console.log(key + ' is ' + value.house); 
       $.each(value.multipleImages, function(key2, image) {    
        console.log('imageId of ' + key2 + ' is ' + image.imageId); 
        console.log('rentImageId of ' + key2 + ' is ' + image.rentImageId); 
        console.log('rentImageName of ' + key2 + ' is ' + image.rentImageName); 
       }); 
      }); 
     } 
    }, 
}); 
+0

遗漏的类型错误:无法读取的不确定 –

+0

@@穆罕默德HAMEDANI,我想你的代码我收到错误 –

+0

我更新了我的答案财产“图像标识”,请尝试再次。 @subikshanM –

0

您可以循环再通过你的属性:

这里有一个的jsfiddle展示的解决方案:https://jsfiddle.net/equbq6rf/

const data = [{ 
    "house":"My house", 
    "multipleImages":[{ 
     "imageId":1, 
     "rentImageId":2, 
     "rentImageName":"image_name.jpg" 
    },{ 
     "imageId":3, 
     "rentImageId":4, 
     "rentImageName":"image_name2.jpg" 
    }] 
},{ 
    "house":"My house number 3", 
    "multipleImages":[{ 
     "imageId":5, 
     "rentImageId":6, 
     "rentImageName":"image_name3.jpg" 
    },{ 
     "imageId":7, 
     "rentImageId":8, 
     "rentImageName":"image_name4.jpg" 
    }] 
}]; 

$.each(data, (key, value) => { 
    console.log(key + ' is ' + value.house); 
    $.each(value['multipleImages'], (imageKey, imageValue) => { 
    Object.keys(imageValue).forEach((property) => { 
     console.log(imageValue[property]); 
    }); 
    }); 
}); 

你可以推进它,但它应该做的伎俩。

希望它能帮助:)

迪伦