0

I have the following JSON structure: -

Afghanistan
    : { latitude: "33.93911", longitude: "67.709953" }
American Samoa
    : { latitude: "-14.270972", longitude: "-170.132217" }
Australia
    : { latitude: "-25.274398", longitude: "133.775136" }

How do I loop through it using Javascript or Jquery and get all the values like Australia, latitude, longitude etc?

I am creating above JSON format from json_encode function in PHP.

Here is the original Array format from which I am creating JSON format: -

array (size=94)
  'India' => 
    array (size=2)
      'latitude' => string '20.593684' (length=9)
      'longitude' => string '78.96288' (length=8)
  'Pakistan' => 
    array (size=2)
      'latitude' => string '30.375321' (length=9)
      'longitude' => string '69.345116' (length=9)
3
  • 1
    use .each() to loop it Commented Mar 13, 2017 at 10:46
  • Possible duplicate of looping over a json object array with jquery Commented Mar 13, 2017 at 10:49
  • @guradio can you please elaborate more or give me any working example on how to use .each loop to get all values like country name, latitude, longitude etc. Commented Mar 13, 2017 at 10:50

2 Answers 2

0

Use the following JS code after fixing your JSON with quotes and commas:

var countries = {
  "Afghanistan": {
    "latitude": "33.93911",
    "longitude": "67.709953"
  },
  "American Samoa": {
    "latitude": "-14.270972",
    "longitude": "-170.132217"
  },
  "Australia": {
    "latitude": "-25.274398",
    "longitude": "133.775136"
  }
}
for (var country in countries) {
  values = countries[country];
  console.log('Country: ', country);
  console.log('Latitude: ', values.latitude);
  console.log('Longitude: ', values.longitude);
}

Sign up to request clarification or add additional context in comments.

2 Comments

How I fix my JSON to use your code?? Because my values are dynamic and I am getting values through Ajax. Right I have more than 20 entries in my JSON. How I fixed that?
You don't need to fix anything if you receive the JSON data from an AJAX call
0

Assuming your var countries holding your AJAX response country JSON. Use below code to solve your problem::

var countries = {
	"Afghanistan": {
		"latitude": "33.93911",
		"longitude": "67.709953"
	},
	"American Samoa": {
		"latitude": "-14.270972",
		"longitude": "-170.132217"
	},
	"Australia": {
		"latitude": "-25.274398",
		"longitude": "133.775136"
	}
}
$.each(countries, function (index, value) {
	console.log("Country=>" + index + ", Latitude=>" + value.latitude + ", Longitude=>" + value.longitude);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

3 Comments

Thanks for your answer. can I talk to you on chat?
@junaidafzal yap why not.
@RanaGosh I am not seeing any chat link on your profile?

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.