使用$ .getJSON以正确的格式从php返回带有emberjs的json数据

我看了看这个教程

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

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

在这个例子中,json的结果是

{ "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); 生成内部级别,即title,url,id等,但不是’items’对象。 我想知道, items对象是来自表名还是通过编码生成的数组数据来创建? 我使用从这个php文件编码为json的数据:

 $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应用程序就像

 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数据作为多级数组。

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

谢谢!

你可以干脆做

  $resultArray = array(); while ($row = mysqli_fetch_assoc($result)) { $resultArray[] = $row; } $resultJSON= json_encode(array("items"=>$resultArray)); 

祝好运