Jump to content

AJAX w/ JSON


sepoto

Recommended Posts

xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200){var obj = $.parseJSON(xmlhttp.responseText);$('#preview').text(xmlhttp.responseText);}}

Some things about this are still unclear to me. I see that when I print the contents of 'xmlhttpd.responseText' I have an array that looks like this:

[{"idlocations":"1","lat":"-84.3132","lng":"34.0399","name":"Buffalo Wild Wings"},{"idlocations":"2","lat":"-84.313","lng":"34.0419","name":"The Cooler (Family Skate Center)"},{"idlocations":"3","lat":"-84.2996","lng":"34.0441","name":"Autoindulgence Car Wash"},{"idlocations":"4","lat":"-84.2881","lng":"34.0511","name":"Residence Inn Alpharetta"},{"idlocations":"5","lat":"-84.2285","lng":"34.0518","name":"Neighborhood Fitness"},{"idlocations":"6","lat":"-84.235","lng":"34.0591","name":"Pepperoni's Tavern"},{"idlocations":"7","lat":"-84.2419","lng":"34.0607","name":"Atlanta Bread Co"},{"idlocations":"8","lat":"-84.2253","lng":"34.0609","name":"Dilworth Coffeehouse"},{"idlocations":"9","lat":"-84.3146","lng":"34.0663","name":"Sky Car Wash"},{"idlocations":"10","lat":"-84.2611","lng":"34.0681","name":"Staybridge Suites"},{"idlocations":"11","lat":"-84.2636","lng":"34.0682","name":"Schlotzsky's Deli"},{"idlocations":"12","lat":"-84.3006","lng":"34.0708","name":"Sanford Rosser & Associates Real Estate"}]

If I change the code to something like this:

xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200){var obj = $.parseJSON(xmlhttp.responseText);$('#preview').text(obj);}}

Then what I end up with as output looks like this:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],

So then what is the proper way to access obj? Did I parse it improperly or am I accessing obj wrongly? I just don't know what I need to do with it yet. Thanks so much for helping! P.S. I am also wondering if there is some significance to the opening '[' and closing ']' that I am seeing in the original 'xmlhttp.onreadystatechange'. Are those brackets really supposed to be there? Error handling with parseJSON is also a source of questions for me at this point. I could use some guidance here.

Link to comment
Share on other sites

Accessing the array like this:

$('#preview').text(obj[0].name);

is one of the key factors I see now.

Link to comment
Share on other sites

It depends what you want to do, I guess. If your only goal is to print the JSON structure then the first example is correct. If you want to have Javascript be able to actually use the array of objects then you need to parse it first. it's not very useful to just print it out, normally you would loop through the array and access each of the objects inside it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...