Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从json数据中公开图像_Javascript_Arrays_Json - Fatal编程技术网

Javascript 如何从json数据中公开图像

Javascript 如何从json数据中公开图像,javascript,arrays,json,Javascript,Arrays,Json,我循环了一些json,并从数据中提取了URL。缩略图数据如下所示: {href: "https://link/medium.jpg"} href: "https://link/medium.jpg" >__proto__: Object 如何公开每个url,使实际图像显示在浏览器上而不是链接上。这是我的密码console.log(o._links.缩略图)是我从上面收到的数据: <html lang="en"> <head> <me

我循环了一些json,并从数据中提取了URL。缩略图数据如下所示:

{href: "https://link/medium.jpg"}
   href: "https://link/medium.jpg"
    >__proto__: Object
如何公开每个url,使实际图像显示在浏览器上而不是链接上。这是我的密码
console.log(o._links.缩略图)
是我从上面收到的数据:

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=\, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Fetch Json</title>
  </head>
  <body>
    <p>
      thumbnail: 
    </p>
    <script>
      const url =
        "https://s3-us-west-2.amazonaws.com/example.json";
      async function getThumbnail() {
        const response = await fetch(url);
        const data = await response.json();
        var art = data._embedded.artworks;
        art.forEach(function(o) {
          //console.log(o._links.thumbnail);
          var img = document.createElement("image");
          img.src = o._links.thumbnail; //set the value equal to the href
          document.querySelector("body").appendChild(img);
    });
  }

  getThumbnail();
</script>

获取Json

缩略图:

常量url= "https://s3-us-west-2.amazonaws.com/example.json"; 异步函数getThumbnail(){ const response=等待获取(url); const data=wait response.json(); var art=数据。_embedded.artworks; 第2条(功能(o){ //console.log(o.\u links.缩略图); var img=document.createElement(“图像”); img.src=o.\u links.缩略图;//将该值设置为href 文件。查询选择器(“正文”)。附录子项(img); }); } getThumbnail();

您需要操作DOM,类似这样的操作

let elem = document.createElement("img");
elem.src = o._links.href;
document.getElementById("placehere").appendChild(elem);
参考:


您需要操作DOM,类似这样的操作

let elem = document.createElement("img");
elem.src = o._links.href;
document.getElementById("placehere").appendChild(elem);
参考:


尝试附加
图像
元素,并将
src
属性设置为
href
这比我之前提出的代码更一般:

1) 循环通过json 2) 创建图像元素

var img=document.createElement(“图像”);
img.src=o.\u links.缩略图//将该值设置为href

文件。查询选择器(“正文”)。附录子项(img)

尝试附加
图像
元素,并将
src
属性设置为
href
这比我之前提出的代码更一般:

1) 循环通过json 2) 创建图像元素

var img=document.createElement(“图像”);
img.src=o.\u links.缩略图//将该值设置为href

文件。查询选择器(“正文”)。附录子项(img)

什么是_embedded.artworks?尝试访问data.href访问图像位置尝试使用?什么是_embedded.artworks?尝试访问data.href访问图像位置尝试使用?当我尝试时,我看到了…因为
图像
元素上次实际上不是元素。我修正了密码。顺便说一句,如果你得到的是object对象,那是因为
o.\u links.thumbnail
实际上并不是指
href
值。您可能需要根据json结构进行设置。我编辑了我的代码供您检查,但现在它实际上没有显示任何内容//将该值设置为href
,如我所说,它可以是
img.src=o.\u links.thumbnail.href当我尝试这样做时,我看到了…因为上次
图像
元素实际上不是一个元素。我修正了密码。顺便说一句,如果你得到的是object对象,那是因为
o.\u links.thumbnail
实际上并不是指
href
值。您可能需要根据json结构进行设置。我编辑了我的代码供您检查,但现在它实际上没有显示任何内容//将该值设置为href
,如我所说,它可以是
img.src=o.\u links.thumbnail.href当我尝试这样做时,我实际上看不到图像。我看到一个小图标,显示图像应该放在那里(我收到一个404),src应该是
o.\u links.thumbnail.href
,当我尝试看不到图像时。我看到一个小图标,显示图像应该放在那里(我收到一个404),src应该是
o.\u links.thumbnail.href