简体   繁体   中英

How do I access the element title in the Json?

I want to GET JSON from http://omadbapi.com/?s= for search script, but I'm having trouble with get Title element in this JSON:

{
    "Search": [{
        "Title": "Sherlock Holmes: A Game of Shadows",
        "Year": "2011",
        "imdbID": "tt1515091",
        "Type": "movie"
    },{
        "Title": "Spy Kids 3-D: Game Over",
        "Year": "2003",
        "imdbID": "tt0338459",
        "Type": "movie"
    }]
}

JavaScript :

$(document).ready(function () {
    var url = 'http://www.omdbapi.com/?',
        mode = 's=',
        input,
        movieName;

    $('button').click(function() {
        var input = $('#movie').val(),
        movieName = encodeURI(input);

        $.getJSON( url + mode + input, function( data ) {
            $.each(data, function(e, p) {
                document.getElementById("item").innerHTML="Title : " + p.Title;
            });
        });
    });
});

How can I retrieve p.Title or data.Title from the returned JSON?

Try like this

$.each(data.Search, function(e,p) {
  document.getElementById("item").innerHTML="Title : " + p.Title;
});

For the first title:

data.Search[0].Title

For the second:

data.Search[1].Title

Fiddle: http://jsfiddle.net/dgrundel/d2m8z3oj/

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.

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