为什么我的PHP脚本没有返回有效的JSON


Why is my PHP script not returning valid JSON

下面链接中的JSON有什么问题???

http://bruceexpress.com/beerstore/test/getottawaeaststorebeerlist.php

使用PHP,我从SQL查询中生成了上面的JSON,并用JSON_encode对其进行了编码。

$retval = mysqli_query($connection, $sql);
if(! $retval )
{
    print('Could not select: ' . mysqli_error());
}
$storearray = array();
while($row = mysqli_fetch_assoc($retval))
{
    $storearray[] = $row;
}
header('Content-Type: application/json');
echo json_encode($storearray);

在SWIFT中:

json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)

正在引发NSCocoaErrorDomain Code=3840异常。"JSON文本未以数组开头……"

我发现了异常并打印了数据,我看到的是:

("<!DOCTYPE html>'n[{'"beer_id'":'"1650'",'"store_id'":'"4618'"},{'"beer_id'":'"1650'",'"store_id'":'"4607'"},{'"beer_id'":'"1650'",'"store_id'":'"4616'"},{'"beer_id'":'"1650'",'"store_id'":'"4604'"},{'"beer_id'":'"5213'", 

(等等。我没有复制整件事。太长了。")

我知道有很多这样的问题,但我仍然没有看到我需要的答案。我的JSON出了什么问题???

首先用UTF-8编码php文件,不带BOM。并删除<?php标签之前的所有空白第二,如果你想发送标题,之前不要打印任何内容;第三,您确实需要在脚本中进行一些检查和返回,因为我现在看到至少有2个错误将出现在未来的中