$。值为json,它是怎么回事?

时间:2011-10-27 19:40:16

标签: javascript jquery json

如何使用$.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>');
                });
            }
)}

4 个答案:

答案 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>');
 });
});

DEMO