1

您好,我有 json 格式的结果,我想将其附加到 html div。json看起来像:

jsonp1373893536648([{
    "MAXCHILDREN": 1,
    "PICTURE": "3AE30747-0956-0F6C-F6F4D6FC13243BDF.jpg",
    "MAXPERSONS": 3,
    "RATEINCLUDE": 2,
}, {
    "MAXCHILDREN": 1,
    "PICTURE": "3AF1F0ED-01DC-32C8-C663F39A1B430F89.jpg",
    "MAXPERSONS": 3,
    "RATEINCLUDE": 2,
}, {
    "MAXCHILDREN": 3,
    "PICTURE": "3AF6A412-CB40-3B77-C777273F5378345A.jpg",
    "MAXPERSONS": 3,
    "RATEINCLUDE": 2,
}, {
    "MAXCHILDREN": 1,
    "PICTURE": "3AFC9046-92F4-DBB7-93E5D7EF8A9AB831.jpg",
    "MAXPERSONS": 3,
    "RATEINCLUDE": 2,
}])

现在我需要例如做这样的事情:

jQuery(response_container).append('<div class="maxchildren">MAXCHILDREN: ' + MAXCHILDREN + '</div>');
jQuery(response_container).append('<div class="pic">PICTURE: ' + PICTURE+ '</div>'); ...

对于每个 MAXCHILDREN、PICTURE、MAXPERSONS、RATEINCLUDE。请帮忙???

4

2 回答 2

4

尝试,假设数据是您正在谈论的响应:

var a = data, b = 0;

while( b < a.length ){

    $('<div/>',{

         'class' : 'maxchildren',
         'text' : 'MAXCHILDREN: ' + a[ b ].MAXCHILDREN

    }).appendTo( response_container );

    $('<div/>',{

         'class' : 'pic',
         'text' : 'PICTURE: ' + a[ b ].PICTURE

    }).appendTo( response_container );

    b++;
}
于 2013-07-16T07:32:36.677 回答
1
for(var i = 0; i < result.length; i++) {
    jQuery(response_container).append('<div class="maxchildren">MAXCHILDREN: ' + result[i].MAXCHILDREN+ '</div>');   
    jQuery(response_container).append('<div class="pic">PICTURE: ' + result[i].PICTURE+ '</div>');
...
}

result是收到时的 json 对象的名称。这取决于你,你给它起的名字。您拥有的对象是一个 json 对象数组,因此您可以通过result[i]. result[i].NAME_OF_KEY您可以通过(ie result[i].MAXCHILDREN) 或result[i]['NAME_OF_KEY'](ie )访问数组的 json 对象的每个元素result[i]['MAXCHILDREN']

于 2013-07-16T07:00:52.153 回答