嗨,我对jquery和json很新,所以请耐心等待。我搜索了论坛并尝试了很多不同的方法来解决我的问题。
我正在尝试循环使用Freebase的多层JSON文件。
$.getJSON(service_url + topic_id + '?callback=?', params, function(topic) {
$("#free").append('<h3>Description</h3><p>'+topic.property['/common/topic/description'].values[0].value+'</p>');
$("#title").prepend('<h2>'+topic.property['/type/object/name'].values[0].value+'<span><a class="socila-link" style="color:#3b5998;" href="'+topic.property['/common/topic/social_media_presence'].values[0].value+'"> <i class="icon-facebook-sign"></i></a><a class="socila-link" style="color:#0084B4" href="'+topic.property['/common/topic/social_media_presence'].values[2].value+'"> <i class="icon-twitter-sign"></i></a></span></h2>')
$("#fb-span1").append('<p><strong>Official Website: </strong><a href="'+topic.property['/common/topic/official_website'].values[0].value+'">'+topic.property['/common/topic/official_website'].values[0].value+'</a></p>');
$("#fb-span1").append('<p><strong>Genre: </strong>'+topic.property['/music/artist/genre'].values[0].text+'</p>');
$("#fb-span1").append('<p><strong>Founded: </strong>'+topic.property['/music/artist/active_start'].values[0].text+'</p>');
$("#fb-span1").append('<p><strong>Hometown: </strong>'+topic.property['/music/artist/origin'].values[0].text+'</p>');
$.each(topic.property, function(i, val) {
$("#fb-span1").append('<p><strong>Genre: </strong>'+val['/music/artist/genre'].values['text']+'</p>');
});
如果我输入像[0]这样的值,我没有问题得到结果,但我似乎无法循环通过它。没有东西得到返回
答案 0 :(得分:0)
除了getJSON调用之外,您不需要任何jQuery来执行此操作。您只需将JSON数据作为常规Javascript数组和字典进行访问即可。因此,要从“Genre”属性值数组输出多个值,您可以像这样使用for循环:
for (var i=0; i<topic.property['/music/artist/genre'].values.length; i++) {
var val = topic.property['/music/artist/genre'].values[i];
$("#fb-span1").append('<p><strong>Genre: </strong>'+val['text']+'</p>');
}