我有代码来获取和解析JSON数组中的数据。它应该为每个数组条目执行它,但它只是为第一个执行它。
$.ajax({
url: url,
dataType: 'json',
success: function(data){
$.each(data, function (key, value){
var mlsnum = value[1];
var address = value[5] + ' ' + value[6] + ' ' + value[7];
var city = value[9];
var state = value[10];
var zip = value[11];
var county = value[12];
var price = value[13];
var sqft = value[17];
var yrblt = value[22];
var bdrms = value[14];
var baths = value[15];
$('#test').html(
'<div class="listing_single">' +
'<div class="listing_img"><a href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' + mlsnum + '"><img class="imgt" src="http://www.universaldynamicmedia.com/sandbox/Images/' + mlsnum + '/HQPhoto-' + mlsnum + '-1.jpeg" /></a></div>' +
'<div class="listing_data">' +
'<div class="listing_single_address "><a class="address_link" href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' + mlsnum + '">' + address + '</a></div>' +
'<div class="listing_single_city">' + city + ', ' + state + ' ' + zip + '</div>' +
'<div class="listing_single_data_spec"><span class="listing_single_price">' + price + '</span>|<span class="data_small">SqFt: ' + sqft + '</span>|<span class="data_small">Year Built: ' + yrblt + '</span>|<span class="data_small"> Bedrooms: ' + bdrms + '</span>|<span class="data_small"> Baths: ' + baths + '</span></div>' +
'</div>' +
'<div class="clear"></div></div>'
);
});
},
error: function(){
alert('There was an error loading your request. <br />Please try again later.');
}
});
阵列采用三维结构......
{ "0" : [
{ "0" : [
{ "0" : "data1", "1" : "data2"}]}
{ "1" : [
{ "0" : "data1", "1" : "data2"}]}
]}
我认为它只是为.each
第一维项目提取数据,其中只有一个。我想我需要将.each
函数深入到第二维,但我不知道该怎么做。
答案 0 :(得分:0)
你可以在每个语句中嵌套jquery吗?如果我理解正确:
$.each(data, function(key, value) {
$.each(value[key], function(key2, value2) {
$.each(value2[key2], function(key3, value3){
// etc.
});
});
});