[Solved] Getting JSON data with AJAX and converting to array of strings


It will depend on what your putting the objects into. The objects you’ve been given are just fine, but in order to access the values inside them you will need to access them via the DisplayName property.

$.each(results, function(i,e) {
    console.log(e.DisplayName);
})

This would log XXX Street Middle School, XXXX Schools, etc.

To circumvent this behavior, do not shove the values into a named part of the array.
I assume you want the result to look similar to:

["XXX Street", "xxxxx school", "xxxx", ... ]

So in your php code, you want to do something like this:

$json = array();
foreach($school in $schools) {
    array_push($json, $school->displayName); //obviously this is unique to your application and not this verbatim
}
echo json_encode($json);

Basically, you want a list, not an associative array. Now, if you had more information you needed to display about a single school you would want an associative array. But given your usage desires, I believe this is closer to what you need.

As an aside, if you use the $.getJSON() function in jQuery (as opposed to $.get), you can avoid using the call to jQuery.parseJSON() and will just get a json object in your success function.

1

solved Getting JSON data with AJAX and converting to array of strings