将Json附加到div

时间:2013-07-16 06:56:09

标签: jquery json html append

您好我的结果是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。 请任何帮助???

2 个答案:

答案 0 :(得分: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++;
}

答案 1 :(得分: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(即result[i].MAXCHILDREN)或result[i]['NAME_OF_KEY'](即result[i]['MAXCHILDREN'])访问数组的json对象的每个元素。