traversing through json objects

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

traversing through json objects

Post 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
Last edited by Luke on Fri Sep 22, 2006 12:30 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

for(var i in someObject)
similar to foreach.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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 :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Well, you can combine multiple for..in loops.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

stab in the dark

Code: Select all

eval(response);
// to
eval('var lljj = ' + response);
for(var i in lljj)...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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}}
Last edited by Luke on Wed Jan 10, 2007 10:57 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's all one single response? There's three high-level objects in there.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yep... OK, so I should probably enclose that stuff in {} ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yup, make it a single object. :)
Post Reply