繁体   English   中英

jQuery.getJSON REST访问数据

[英]jQuery.getJSON REST access data

在这个json rest对象上:

[{"id_film":1,"title":"Forrest Gump","director":"Zameckis","year":"1994","price":"12.00","format":"DVD"},{"id_film":4,"title":"Back to the Future II","director":"Zameckis","year":"1989","price":"9.95","format":"DVD"}]

我正在尝试使用$ getJSON函数在网页中构建响应并将响应放置在div id =“ films”中,但是我不知道如何获取“ title”和“ director” json属性。

$.getJSON( "http://rest/url", function( data ) {
    var items = [];
    $.each( data, function( title, director ) {
    items.push( "<li id='" + title + "'>" + director + "</li>" );
    });

    $( "<ul/>", {
        "class": "my-new-list",
         html: items.join( "" )
         }).appendTo( "#films" );
   });

在响应中仅显示以下内容的无序列表:

[object Object]
[object Object]

我该怎么做才能获得“头衔”和“导演”无序列表?

谢谢

尝试这个

$.each( data, function( index, value ) {
    items.push( "<li id='" + value.title + "'>" + value.director + "</li>" );
});

each回调都需要两个参数indexelement 变量director包含完整的对象,因此您可以获取[object Object] (从.toString()返回,因为您尝试将object转换为String ),因此需要从object获取property ,可以像在我的示例中那样进行操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM