如何使用Javascript在Json中获取图像值

时间:2018-01-31 23:06:46

标签: json

{  
   "\/dom-12239-1-cover-band-2018-greg-goodloe":{  
       "server":"ia601507.us.archive.org",
       "dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
       "misc":{  
          "image":"https:\/\/ia601507.us.archive.org\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe\/dom-12239-1-cover-band-2018-greg-goodloe.thumbs\/dom-12239-1-cover-band-2018-greg-goodloe_000001.jpg",
          "collection-title":"Denver Open Media"
       }
   }
}

这是我的Json文件,如何使用JS获取图像值? " / DOM-12239-1盖带-2018-格雷格-GOODLOE"不会总是一样的 谢谢!

2 个答案:

答案 0 :(得分:1)

解决方案

根据您对更改字段名称的要求进行了编辑。

for (var k in obj) {
   var image = obj[k].misc.image;
}

obj是你的JSON对象。

证明

function mySolution(obj) {
  for (var k in obj) {
    var image = obj[k].misc.image;
    console.log(image);
  }
}

var obj = {  
   "\/dom-12239-1-cover-band-2018-greg-goodloe":{  
       "server":"ia601507.us.archive.org",
       "dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
       "misc":{  
          "image":"https:\/\/ia601507.us.archive.org\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe\/dom-12239-1-cover-band-2018-greg-goodloe.thumbs\/dom-12239-1-cover-band-2018-greg-goodloe_000001.jpg",
          "collection-title":"Denver Open Media"
       }
   }
}

mySolution(obj);

obj = {  
   "\/dom-sdasdfasdfasdfasdf-1-cover-band-2018-greg-goodloe":{  
       "server":"ia601507.us.archive.org",
       "dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
       "misc":{  
          "image":"https:\/\/ia601507.us.archive.org\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe\/dom-12239-1-cover-band-2018-greg-goodloe.thumbs\/dom-12239-1-cover-band-2018-greg-goodloe_000001.jpg",
          "collection-title":"Denver Open Media"
       }
   }
}

mySolution(obj);

答案 1 :(得分:0)

因为您不知道密钥是什么,所以需要为每个密钥循环JSON。然后,您可以使用ObjectID输出图像,因为json[key].misc.image位于image内。

这可以在以下内容中看到:



misc




希望这有帮助! :)