简体   繁体   English

如何访问Json中的元素标题?

[英]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: 我想从http://omadbapi.com/?s=获取JSON作为搜索脚本,但是在此JSON中获取Title元素时遇到了麻烦:

{
    "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 : 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? 如何从返回的JSON中检索p.Titledata.Title

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/ 小提琴: http : //jsfiddle.net/dgrundel/d2m8z3oj/

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

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