0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.