Jump to content

Working with JSON returned from PHP by json_encode();


sepoto

Recommended Posts

[{"lat":"34.1721","lng":"-118.947","company":"The Bar Cave","street_address1":"XXX Eagle Ridge St.","city":"My City","state":"CA","postal_code":"90007","phone_number":"8055551212","email":"eric.sepich@hotmail.com","website":"","id":"10","twitter":""}]

The first thing that strikes me about how this JSON was returned from PHP is the opening and closing '[' and ']'. So I am trying to figure out how to iterate through this array which actually represents one row of data from my database. I am wondering do I need to first call $.parseJson();? Do the '[' and ']' remain in there? A lot of examples I see don't have those in the JSON. My JavaScript is pretty simple when it comes back it looks like this:

$.ajax({ url: 'ajax.php',		 data: { lat: results[0].geometry.location.lat(), lng: results[0].geometry.location.lng(), radius: radius },		 type: 'post',		 success: function(output) {					  alert(output);					  /* So now that we have the JSON encoded array it					  has to be displayed into one of the div's probably					  below the map someplace */				  }		});

Essentially I am trying to determine the bounds of the JSON array and iterate through each item. So I know for this one that it is one row and I see the elements but it will of course be different each time. What is the accepted way to do this? So I am also familiar with $.each but it does not seem to be processing correctly the array which comes out broken up into bits. Thanks so much....

Edited by sepoto
Link to comment
Share on other sites

I figured it out. My first entry comes out like this: lat 34.1721 from:

   	 $.each( {"lat":"34.1721","lng":"-118.947","company":"The Bar Cave","street_address1":"315 Eagle Ridge St.","city":"Newbury Park","state":"CA","postal_code":"91320","phone_number":"8054028072","email":"eric.sepich@hotmail.com","website":"","id":"10","twitter":""}, function(k, v){		alert(k + " " + v );		});

It is a bit tricky the way the examples are but it works way well. Sorry... It must be the midnight oil I am burning... So it looks to me like the [ ]'s need to be stripped off in order for the function to work properly. If someone could enlighten me on why the [ ]'s I would really love that and this question would have been usefull to ask then. Thanks guys...

Edited by sepoto
Link to comment
Share on other sites

I believe at this point that the answer to the [ ]'s is in the PHP json_encode doc's under JSON_FORCE_OBJECT. I'm still toying with it. I'm not sure how I came all unraveled... Darn... No.... using JSON_FORCE_OBJECT breaks the $.each.... I'm lost at this point still and I know it all hinges on the [ ]'s which need to come out on the PHP side. Alright.... This took me a while but this is how it should be.... The JSON has to be parsed and then it becomes much easier...

   	 $.ajax({ url: 'ajax.php',         data: { lat: results[0].geometry.location.lat(), lng: results[0].geometry.location.lng(), radius: radius },         type: 'post',         success: function(output) {                    var obj = $.parseJSON(output);                    alert(obj[0].lat);                    /* So now that we have the JSON encoded array it                    has to be displayed into one of the div's probably                    below the map someplace */                                       	                    }        });

Edited by sepoto
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...