Page 1 of 1

traversing through json objects

Posted: Fri Sep 22, 2006 12:16 pm
by Luke
I have a php page that echoes out a bunch of json objects. I want for javascript to perform an XMLHTTPRequest to grab this data... that's all find and good... done... but how do I now traverse through these with javascript? The json objects look something like this:

Code: Select all

{"point" : [132, 30.000, -123.120397], "business" : {"name" : "The Business", "address" : "1234 Some road"}}
Is there an easy way to basically traverse through these and process the data? I'm waiting on the new O'reilly Javascript book to come in the mail, but in the mean time, you guys are my only help. 8O

Posted: Fri Sep 22, 2006 12:29 pm
by feyd

Code: Select all

for(var i in someObject)
similar to foreach.

Posted: Fri Sep 22, 2006 12:32 pm
by Luke
OK, but the results are going to be X number of these objects one after the other... what would i use as the iterator? I'm sorta lost :?

Posted: Fri Sep 22, 2006 12:44 pm
by feyd
Well, you can combine multiple for..in loops.

Posted: Fri Sep 22, 2006 12:51 pm
by Luke
hmm... either I'm not understanding you or you're not understanding what I'm trying to ask (probably both). Let me post some code... maybe that will clear things up :D

Code: Select all

function findLocations(conditions){
	var conditions = conditions ? conditions : '';
	var request = GXmlHttp.create();
	request.open("GET", "<?php echo $_GET['url']; ?>/map/find" + conditions, true);
	request.onreadystatechange = function() {
	  if (request.readyState == 4) {
		var locations = convertLocationResponse(request.responseText);
	  }
	}
	request.send(null);
}
function convertLocationResponse(response){
        var locations = new Array();
	eval(response);
// Traverse through response, which is just a bunch of json objects in a row
for(var i in /* What goes here? */){
    locations[name] = i;
}
        return locations;
}

Posted: Fri Sep 22, 2006 1:00 pm
by feyd
stab in the dark

Code: Select all

eval(response);
// to
eval('var lljj = ' + response);
for(var i in lljj)...

Posted: Fri Sep 22, 2006 1:46 pm
by Luke
sigh... I'm still having a hard time... am I going about this wrong? Here is my exact response code... so maybe somebody can tell me either how to traverse this data (I realize you already have, feyd, but I'm not getting it) or whether or not I'm even going about this logically.

Code: Select all

{"point" : [74, 35.7000007629, -121.5732498169], "business" : {"id" : 30, "name" : "Luke's Crip2", "owner" : "", "contact_name" : "Luke Visinoni", "contact_title" : "", "address" : "1234 Some Way", "address2" : "", "city" : "Pa", "state" : "CA", "zip" : 95969, "phone" : 53677, "alt_phone" : "", "fax" : "", "email" : "lukgn.com", "website" : "", "description_short" : "", "description" : "Some stuff", "season" : "", "hours" : "", "created" : 1158942874, "modified" : 1158942874, "suspended" : "", "county" : "", "type" : "", "keywords" : "", "corridor" : "hwy70", "point_id" : 74}}

{"point" : [72, 30.0000000000, -120.0000000000], "business" : {"id" : 29, "name" : "Luke's Crip", "owner" : "", "contact_name" : "Luke Visinoni", "contact_title" : "", "address" : "", "address2" : "", "city" : "Pa", "state" : "C", "zip" : 95969, "phone" : 5308721677, "alt_phone" : "", "fax" : "", "email" : "l@esign.com", "website" : "", "description_short" : "", "description" : "Some crap", "season" : "", "hours" : "", "created" : 1158940161, "modified" : 1158940161, "suspended" : "", "county" : "", "type" : "", "keywords" : "", "corridor" : "hwy70", "point_id" : 72}}{"point" : [73, 38.1115837097, -121.5732498169], "business" : {"id" : 28, "name" : "The New Splace", "owner" : "", "contact_name" : "Luke Visinoni", "contact_title" : "", "address" : "50n Way", "address2" : "", "city" : "Par", "state" : "CA", "zip" : 69, "phone" : 53077, "alt_phone" : "", "fax" : "", "email" : "lun.com", "website" : "", "description_short" : "", "description" : "Some stuff", "season" : "", "hours" : "", "created" : 1158940000, "modified" : 1158940000, "suspended" : "", "county" : "", "type" : "", "keywords" : "", "corridor" : "hwy99", "point_id" : 73}}

{"point" : [71, 11.0000000000, 99.0000000000], "business" : {"id" : 27, "name" : "Gary Putty", "owner" : "", "contact_name" : "Luke Visinoni", "contact_title" : "", "address" : "04ay", "address2" : "", "city" : "Paradise", "state" : "CA", "zip" : 95969, "phone" : 530877, "alt_phone" : "", "fax" : "", "email" : "luk@ign.com", "website" : "", "description_short" : "", "description" : "Some stuff", "season" : "", "hours" : "", "created" : 1158939652, "modified" : 1158939652, "suspended" : "", "county" : "", "type" : "", "keywords" : "", "corridor" : "i5", "point_id" : 71}}

Posted: Fri Sep 22, 2006 2:07 pm
by feyd
That's all one single response? There's three high-level objects in there.

Posted: Fri Sep 22, 2006 2:31 pm
by Luke
yep... OK, so I should probably enclose that stuff in {} ?

Posted: Fri Sep 22, 2006 2:52 pm
by feyd
Yup, make it a single object. :)