以正确的格式使用$ .getJSON返回来自php for emberjs的json数据 [英] return json data from php for emberjs in the right format with $.getJSON

查看:119
本文介绍了以正确的格式使用$ .getJSON返回来自php for emberjs的json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了这个教程

,并尝试创建自己的PHP脚本来生成json数据。

and tried to create my own php side script to generate json data.

但是,我失败了在创建一个多级数组。

However, I failed on creating a multi-level array.

在这个例子中,一个json结果

In the example, a json result of

{
  "nextId": null,
  "items": [{
          "title": "Docker, the Linux container runtime: now open-source",
          "url": "http://docker.io",
          "id": 5445387,
          "commentCount": 39,
          "points": 146,
          "postedAgo": "2 hours ago",
          "postedBy": "shykes"
      }, {
          "title": "What\u0027s Actually Wrong with Yahoo\u0027s Purchase of Summly",
          "url": "http://hackingdistributed.com/2013/03/26/summly/",
          "id": 5445159,
          "commentCount": 99,
          "points": 133,
          "postedAgo": "2 hours ago",
          "postedBy": "hoonose"
      },
  ],
  "version": "1.0",
  "cachedOnUTC": "\/Date(1364333188244)\/"
}

生成,我可以使用 $ resultJSON = json_encode($ resultArray); 来生成内部级别,这是标题,url,id等,而不是'items'对象。我想知道,项目对象是否来自表的名称,还是通过对数组中生成的数组数据进行编码而创建?
我使用从这个php文件编码为json的数据:

was generated, I could use $resultJSON= json_encode($resultArray); to generate the inner level, which is title, url, id and such, but not 'items' object. I want to know, whether the items object comes from the table's name or it is created by encoding a generated array data from an array? I use data encoded as json from this php file:

$query = "SELECT user_id, username, legacy_id, date_created
FROM eq_user";
$result = mysqli_query($connection, $query);
$numRows = mysqli_num_rows($result);
$resultArray = mysqli_fetch_assoc($result);
$resultJSON= json_encode($resultArray);
echo $resultJSON;

emberjs应用程序将像

The emberjs app will be like

App.Item.reopenClass({
  all: function() {
      return $.getJSON("/get_data.php").then(function(response) {
        var items = [];                     
        response.items.forEach( function (item) {
          items.push( App.Item.create(item) );
        });

          return items;
      });
  }
});

注意:项目是我认为的对象,但我不知道如何正确生成在php中作为json数据作为多级数组。

NOTE: the items is the object I think, but I do not know how it can be generated correctly in php as json data as multi-level array.

另外还有一个问题,我应该使用json_encode吗?因为我看到教程提到Emberjs使用jsonp ...是一种特殊的格式?我应该如何在PHP中处理这个jsonp?

Also, one more question, should I use json_encode or not? Because I saw tutorial mentioning that Emberjs uses jsonp... is it a special format? How should I approach this jsonp in PHP?

谢谢!

推荐答案

你可以简单地做

 $resultArray = array();
 while ($row = mysqli_fetch_assoc($result)) {
    $resultArray[] = $row;
 }

 $resultJSON= json_encode(array("items"=>$resultArray));

祝你好运

这篇关于以正确的格式使用$ .getJSON返回来自php for emberjs的json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆