Hi,
Im having trouble trying to access some values of the response from an API service. My code to connect and output for each item is working Ok getting the name and age. But I can’t work out how to get the image urls. Clutching at straws I thought I could just target the first value stored using $imageURL = item.images[‘url’][0] but that fails.
$.getJSON("http://api-address", function(data) {
var html = '';
$.each(data.response.artists, function(i, item) {
html += "<h2 " + item.name + "</h2>"
$imageURL = item.images['url'][0]
html += "<img src=\\"" . $imageURL + "\\" >"
$('#page').append(html); // add top artists to page
});
The output from the API is formatted like this.
{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"},
"employees": [
{
"name": "bob",
"age": 42,
"images": [
{"url": "http://myserver.com/_/one.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
{"url": "http://myserver.com/_/two.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
{"url": "http://myserver.com/_/three.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
{"url": "http://myserver.com/_/four.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
{
"name": "Jimbo",
"age": 50,
"images": [
{"url": "http://myserver.com/_/one.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
{"url": "http://myserver.com/_/two.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
{"url": "http://myserver.com/_/three.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
{"url": "http://myserver.com/_/four.jpg", "license": {"type": "unknown", "attribution": "", "url": ""}},
I only require one image but it would be good randomise which one is was (though I might be able to work that out myself is I could just work out how to target them!
Any help would be appreciated!