0

I want to display JSON result in HTML table using plain JavaScript without using JQuery or any other libraries, I have JSON response like below:

{
  "topalbums": {
    "album": [
      {
        "name": "Konvicted",
        "playcount": "61503",
        "mbid": "13141107-58e2-4984-bb90-55aa59fc03f6",
        "url": "http://www.last.fm/music/Akon/Konvicted",
        "artist": {
          "name": "Akon",
          "mbid": "1138a764-2212-4d0a-b02d-0dc14df91e08",
          "url": "http://www.last.fm/music/Akon"
        },
        "image": [
          {
            "#text": "http://userserve-ak.last.fm/serve/34s/55550849.png",
            "size": "small"
          },
          {
            "#text": "http://userserve-ak.last.fm/serve/64s/55550849.png",
            "size": "medium"
          },
          {
            "#text": "http://userserve-ak.last.fm/serve/126/55550849.png",
            "size": "large"
          },
          {
            "#text": "http://userserve-ak.last.fm/serve/300x300/55550849.png",
            "size": "extralarge"
          }
        ],
        "@attr": {
          "rank": "1"
        }
      },

I want to access the first image URL as seen above, I tried using it like below but did not worked:

var text;
for (i = 0; i < json.topalbums.album.length; i++) 
{
  text += json.topalbums.album[i].name + " "+json.topalbums.album[i].image[1].#text+"<br>";
}

Can anyone suggest some solution?.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Salman Siddique
  • 90
  • 1
  • 1
  • 8

1 Answers1

1

You can use ["#text"] to access it:

var text;
for (i = 0; i < json.topalbums.album.length; i++) 
{
  text += json.topalbums.album[i].name + " "+json.topalbums.album[i].image[1]["#text"]+"<br>";
}
Green Su
  • 2,318
  • 2
  • 22
  • 16