Solved

How do I traverse this multidimensional JSON array/obj

Posted on 2009-05-18
1
2,387 Views
Last Modified: 2013-11-08
I'm creating an AJAX script with PHP, and JS.

The PHP takes GET parameters, returns some data from the database. I need this data to be usable through javascript. My php verison < 5.2 so I dont have JSON encode. So far i've been able to get PHP to format a JSON array, the data looks like the following

{"6":{"lat":"34.409425","lng":"-119.861470"},"5":{"lat":"34.409414","lng":"-119.861221"},"8":{"lat":"34.410934","lng":"-119.861718"},"10":{"lat":"34.410503","lng":"-119.863998"},"9":{"lat":"34.412089","lng":"-119.866134"},"3":{"lat":"34.409475","lng":"-119.854704"},"7":{"lat":"34.413153","lng":"-119.855430"},"15":{"lat":"34.414979","lng":"-119.712181"},"14":{"lat":"34.414979","lng":"-119.712181"},"11":{"lat":"34.414979","lng":"-119.712181"},"13":{"lat":"34.414979","lng":"-119.712181"}}

The first key is a uuid then the following key is another array with latitude and logitude.  I need to extract this information from javascript.
function get_map()
{
	var distance = $("#distance").val();
	var sort = $("input[name='sort']:checked").val(); 
	var who = $("input[name='who']:checked").val();
	
	$.getJSON("ajax/ajax_get_locations_map.php",{distance: ""+distance+"",who: ""+who+"",sort: ""+sort+""}, function(data){
		var obj = (data);
		$.each(obj, function(i, val) {
 
		     //need help at this point to get variables  
		    });
	});
}

Open in new window

0
Comment
Question by:bperin42
1 Comment
 
LVL 12

Accepted Solution

by:
alien109 earned 500 total points
ID: 24418974
you can do this:


for(var uuid in data) {
    var lat = data[uuid].lat;
    var lng = data[uuid].lng;
}
 
//Here's what your json string looks like formatted, revealing the structure a little better
 
{
   "6":{
      "lat":"34.409425",
      "lng":"-119.861470"
   },
   "5":{
      "lat":"34.409414",
      "lng":"-119.861221"
   },
   "8":{
      "lat":"34.410934",
      "lng":"-119.861718"
   },
   "10":{
      "lat":"34.410503",
      "lng":"-119.863998"
   },
   "9":{
      "lat":"34.412089",
      "lng":"-119.866134"
   },
   "3":{
      "lat":"34.409475",
      "lng":"-119.854704"
   },
   "7":{
      "lat":"34.413153",
      "lng":"-119.855430"
   },
   "15":{
      "lat":"34.414979",
      "lng":"-119.712181"
   },
   "14":{
      "lat":"34.414979",
      "lng":"-119.712181"
   },
   "11":{
      "lat":"34.414979",
      "lng":"-119.712181"
   },
   "13":{
      "lat":"34.414979",
      "lng":"-119.712181"
   }
}

Open in new window

0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checking data with javascript 3 19
jQuery - following an example but can I save data server side? 20 49
Jquery keyup 4 18
How do I pull the base url for use in html links 7 23
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question