2014-08-01 15:17
浏览 580


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);

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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将我的数据放入一个字符串数组中,以便我可以将它用作jquery的数据集 自动完成。 我现在得到的是一系列对象,我认为这些对象无效。


 <  code> $。get(“get_schools.php”,function(data){
 var results = jQuery.parseJSON(data); 

这会产生以下格式的数据: [对象对象],[对象对象],[对象对象]

在get_schools.php上: nI查询数据库并将结果返回给名为$ displaynames的数组。 然后:

  echo json_encode($ displaynames); 

如下所示: [{“DisplayName “:”XXXX Street Middle School“},{”DisplayName“:”XXXXX Schools“},{”DisplayName“:”XXXX Elementary School“}

更新: 这个问题已被标记为可能重复,但另一个问题涉及获取对象的属性。 我并没有试图得到一个物体的财产。 我试图将对象数组转换为字符串数组。 我需要属性名称以及结果中的值。 另一个问题只返回值。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dou426098 2014-08-01 15:30

    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) {

    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.

    解决 无用
    打赏 举报
  • dongxuan1660 2014-08-01 15:33
    $.get("get_schools.php", function(data) {
               var results = jQuery.parseJSON(data).map(function (item) {
                   return item.DisplayName;

    results will be an array of strings

    ["XXXX Street Middle School", "XXXXX Schools", "XXXX Elementary School"]
    解决 无用
    打赏 举报

相关推荐 更多相似问题