1

Can any body help, m totally new in Json I want to fetch the image but m bit confuse in the array and object thing. Or is it possible to fetch image without img tag. Thank you in advance

This is my JSON link

this what I have tried:

function myFunction(worldpopulation) {
    var out = "";
    var i;
    for(i = 0; i<worldpopulation.length; i++)
        out += '' + worldpopulation[i].rank;
    document.getElementById("id01").innerHTML = out;
}

just help me to fetch "rank" only

{ "worldpopulation": 
    [
         {
         "rank":1,"country":"China",
         "population":"1,354,040,000",
         "flag":"http://www.androidbegin.com/tutorial/flag/china.png"
         }, 

         {
         "rank":2,"country":"India",
         "population":"1,210,193,422",
         "flag":"http://www.androidbegin.com/tutorial/flag/india.png"
         }, 

         {
         "rank":3,"country":"United States",
         "population":"315,761,000",
         "flag":"http://www.androidbegin.com/tutorial/flag/unitedstates.png"
         }, 

    ]
}
1
  • please include a small part of your JSON as example in your question, or it will become obsolete and useless to other SO users as soon as this .txt/json file gets removed from the server... It is also quite unclear to me what you're exactly trying to achieve and what are the current and expected result. Commented Jul 16, 2015 at 9:46

3 Answers 3

1

The first thing you will get the content of your json content from this page using XMLHttpRequest

var request = new XMLHttpRequest();
var url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
request.onreadystatechange = function() {
    if(request.readyState == && request.status == 200) {
        var myArr = JSON.parse(xmlhttp.responseText);
        myFunction(myArr);
    }
}

the implementation of myFunction

function myFunction(myArr)
{
    for(var i=0; i< myArr.length; ++i) {
        var img ; // get your element you want to place img inside
        img.src = myArr[0].flag;
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

0

you can use .filter() to get image array.

var imageArr = [];
   imageArr = worldpopulation.filter(function(val,indx){
   return val.flag;
});  

Comments

0

you can use jquery in following way

$.getJSON( "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt",function( data ) { 

  $.each( data.worldpopulation, function( key, country) {
   console.log('Image Url',country.flag)
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.