SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get value from second level object

    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.

    Code:
        $.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.

    Code:
    {"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!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You almost got it, you just need to switch around the url and 0 keys.

    Code:
    $imageURL = item.images[0].url;

  3. #3
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! It's too late to try your suggestion tonight but I'll try tomorrow.

    Thanks again!

  4. #4
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked a treat thank you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •