简体   繁体   English

使用jQuery从JSON显示图像

[英]Display an image from JSON using jQuery

I have the following jQuery code at the moment. 我目前有以下jQuery代码。

Note that to run the snippet here you need to click and allow this first: 请注意,要在此处运行代码段,您需要先单击并允许此代码:

https://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topTvEpisodes/json https://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topTvEpisodes/json

 (function() { var iTunesAPI = "https://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topTvEpisodes/json"; $.getJSON(iTunesAPI) .done(function(data, textStatus) { $.each(data.feed.entry, function(i, ent) { $("<li>" + ent.title.label + "</li>").appendTo("#tvshow"); }); console.log("Yippee " + textStatus); }) .fail(function(jqxhr, textStatus, error) { var err = textStatus + ", " + error; console.log("Request Failed: " + err + " " + jqxhr.status); }) .always(function() { console.log("Whatever"); }) })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <ul id="tvshow"> <ul> 

This displays a list of TV shows with the titles of the TV shows which are numbered into a list. 这将显示电视节目列表,其中包含电视节目的标题,这些节目编号为列表。 I am currently trying to find a way to extract the image from the JSON using jQuery but I am unable to do so. 我目前正在尝试使用jQuery找到从JSON中提取图像的方法,但我无法这样做。 I thought I could just add 我以为我可以补充一下

+ ent.title.image

Into the line with the <li> tags but this then just returns a list saying "undefined". 使用<li>标签进入行,但这只是返回一个说“未定义”的列表。

Any help pointing me in the right direction of how I can extract images would be grateful, I am new to jQuery so learning as I go along. 任何指导我正确指向我如何提取图像的帮助都会感激不尽,我是jQuery的新手,所以随着我的学习。

Thanks, Scott 谢谢,斯科特

The property which has the images is im:image . 具有图像的属性是im:image So, you need something like below: 所以,您需要以下内容:

$.each( data.feed.entry, function( i, ent ) {
  var imgUrl = ent['im:image'][ ent['im:image'].length - 1 ].label, // the last index returns the highest resolution.
  img = '<img src="' + imgUrl + '"/>';
  $("<li>" + ent.title.label + '<br/>' + img + "</li>").appendTo("#tvshow");
});

you should call image from JSON like 你应该从JSON调用图像

ent["im:image"][0].label

for example: 例如:

$("<li><img src='"+ ent["im:image"][0].label +"'/>" + ent.title.label + "</li>").appendTo("#tvshow");

Note: I have change the service call from http to https to demo over https fiddle 注意: 我已将服务调用从http更改为https,更改为https小提琴演示

 (function() { var iTunesAPI = "//ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topTvEpisodes/json"; $.getJSON( iTunesAPI ) .done(function( data, textStatus ) { $.each( data.feed.entry, function( i, ent ) { $("<li><img src='"+ ent["im:image"][0].label +"'/>" + ent.title.label + "</li>").appendTo("#tvshow"); }); console.log( "Yippee " + textStatus ); }) .fail(function( jqxhr, textStatus, error ) { var err = textStatus + ", " + error; console.log( "Request Failed: " + err + " " + jqxhr.status ); }) .always(function() { console.log( "Whatever" ); }) })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="tvshow"> </ul> 

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

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