I have the following code, the first part of which gets JSON from PHP after a drop down selection is triggered. The result is placed in a div called "results" just to verify that it works. Everything looks good up to that point. I also have a couple lines of code which parse the JSON, and display the individual elements in a sequence of alerts, again, just to verify that it functions up to this point, which it does. However, after this, I try to parse the JSON in a similar manner as in creating the alerts, only place the individual results in separate arrays. I have a check to alert the results as the $.each is executed, so that I can see if the code block is functioning. However, it does not work. I am guessing that perhaps my brackets are not setup properly, or perhaps I need to declare another function somewhere. Can anyone suggest what might be the problem?
Thanks for your help.
$(document).ready(function() {
$('#routesDropDown').change(function() {
var queryParam = 'routesStations=' +$(this).val();
$.post('getstations.php', queryParam, processResponse);
});
function processResponse(data) {
$('#results').html(data);
// parse the data and store in variable, "obj"
var obj = jQuery.parseJSON( data );
// iterate through each value in the "obj" variable
$.each(obj, function(key, value) {
alert(value.color + ' ' + value.abbr + ' ' + value.lat + ' ' + value.lng);
});
//THIS IS THE PART THAT DOES NOT WORK...
var obj = jQuery.parseJSON( data ) {
var color = [];
var abbr = [];
var lat = [];
var lng = [];
$.each(obj, function(value) {
color.push(value.color);
abbr.push(value.abbr);
lat.push(value.lat);
lng.push(value.lng);
alert(value.color + ' ' + value.abbr + ' ' + value.lat + ' ' + value.lng);
})};
});
JSONstringdata