Upon this json rest object:
[{"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"}]
I am trying to build a response in a web page using the $getJSON function and place the response in a div id="films", but I do not know how to get to the 'title' and 'director' json properties.
$.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" );
});
In the response only appears an unordered list of:
[object Object]
[object Object]
What I could do to get a list of 'title' and 'director' unordered list?
Thanks
Try this
$.each( data, function( index, value ) {
items.push( "<li id='" + value.title + "'>" + value.director + "</li>" );
});
each
callback expects two arguments index
, element
. Variable director
contains full object so you get [object Object]
(returns from .toString()
, because you try convert object to String
), you need get property
from object, you can do it like in my example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.