-7

I'm trying to get my data into an array of strings so that I can use it as a dataset for jquery autocomplete. What I'm getting now is an array of objects, which I don't think will work.

Jquery on my main page:

$.get("get_schools.php", function(data) {
               var results = jQuery.parseJSON(data);
               alert(results);
});

This results in data in the format of: [object Object],[object Object],[object Object]

On get_schools.php: I query a database and the return the results to an array called $displaynames. Then:

echo json_encode($displaynames);

Which looks like: [{"DisplayName":"XXXX Street Middle School"},{"DisplayName":"XXXXX Schools"},{"DisplayName":"XXXX Elementary School"}

UPDATE: this question has been flagged as a possible duplicate, but the other question pertains to getting a property of an object. i wasn't attempting to get a property of an object. i was attempting to convert an object array to an array of strings. i needed the property names as well as the values in the result. the other question only returns the values.

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
  • "which I don't think will work." Is this a question. OR information? – Raab Aug 01 '14 at 15:20
  • How do you build the `$displaynames` var at PHP? I think there is where your error is – ReynierPM Aug 01 '14 at 15:22
  • exactly what is *the correct format* according to you? Also you should use `console.log()` for debugging. It's far more useful and informative. Especially when using objects. – Novocaine Aug 01 '14 at 15:22
  • Your data seems ok. Just use `console.log(results)` to view ist content - `alert` only works with an explicit string. – LeGEC Aug 01 '14 at 15:25
  • are you people not reading the first sentence? "I'm trying to get my data into an array of strings so that I can use it as a dataset for jquery autocomplete. " – devlin carnate Aug 01 '14 at 15:26
  • So iterate over the array and get the data you want out of each object. – Quentin Aug 01 '14 at 15:28
  • great. i switched from alert to console.log. i still have an array of objects. – devlin carnate Aug 01 '14 at 15:28
  • possible duplicate of [How to get object property from each object in an array?](http://stackoverflow.com/questions/6462150/how-to-get-object-property-from-each-object-in-an-array) – canon Jun 23 '15 at 15:37

2 Answers2

2
$.get("get_schools.php", function(data) {
           var results = jQuery.parseJSON(data).map(function (item) {
               return item.DisplayName;
           });
           alert(results);
});

results will be an array of strings

["XXXX Street Middle School", "XXXXX Schools", "XXXX Elementary School"]
wes
  • 168
  • 1
  • 11
1

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.

Ben Potter
  • 321
  • 1
  • 4