如何使用$.each
跟踪json代码(名称):
1: 11 11
2: 666666666 99999 777777 1221
3: 55555 00000000 222222222 333333333
{
"reunits": [
{
"reun": [
{
"name": "11",
"price": "77192276",
"extra": "11",
"hotel_id": "77192276"
},
{
"name": "11",
"price": "77192276",
"extra": "11",
"hotel_id": "77192276"
}
]
},
{
"reun": [
{
"name": "666666666",
"price": "15190364",
"extra": "11",
"hotel_id": "15190364"
},
{
"name": "99999",
"price": "15190364",
"extra": "11",
"hotel_id": "15190364"
},
{
"name": "777777",
"price": "15190364",
"extra": "11",
"hotel_id": "15190364"
},
{
"name": "1221",
"price": "15190364",
"extra": "11",
"hotel_id": "15190364"
}
]
},
{
"reun": [
{
"name": "55555",
"price": "11",
"extra": "33",
"hotel_id": "15183965"
},
{
"name": "00000000",
"price": "11",
"extra": "33",
"hotel_id": "15183965"
},
{
"name": "222222222",
"price": "11",
"extra": "33",
"hotel_id": "15183965"
},
{
"name": "333333333",
"price": "11",
"extra": "33",
"hotel_id": "15183965"
}
]
}
]
}
我的尝试就是这个(不行):
$.ajax({
...
success: function (data) {
$.each(data.reunits['reun'], function (index, value) {
$('.li_show').append('<li>'+value.name+'</li>');
});
}
)}
答案 0 :(得分:2)
$.each(data.reunits, function (index, value) {
var parts = [];
$.each(value.reun, function(k,v){
parts.push(v.name);
});
$('.li_show').append('<li><b>' + (index + 1) + ':</b> ' +parts.join(" ")+'</li>');
});
答案 1 :(得分:0)
您可以执行以下操作:
$.ajax({
...
success: function (data) {
$.each(data.reunits, function (index, value) {
for(key in value){
$.each(value[key], function(i, v){
$('.li_show').append('<li>'+v.name+'</li>');
});
}
});
)}
您可以使用for
语句
答案 2 :(得分:0)
reunits
包含具有reun
属性的对象,该属性是对象数组。首先需要遍历reunits
数组,然后遍历每个reun
数组。
$.each(data.reunits, function(){
var str = '';
$.each(this.reun, function(){
str += this.name+' ';
});
$('.li_show').append($.trim(str));
});
答案 3 :(得分:0)
$(data.reunits).each(function(i,j){
$.each(j,function(x,y){
$.each(y,function(f,g){
console.log(g.name);
$('.li_show').append('<li>'+g.name+'</li>');
});
});
});
修改强>
$(data.reunits).each(function(i,j){
$.each(j,function(x,y){
$var=" ";
$.each(y,function(f,g){
$var+=" "+g.name;
//console.log(g.name);
});
$('.li_show').append('<li>'+$var+'</li>');
});
});