下面的链接中提供的JSON有什么问题???为什么我的PHP脚本不能返回有效的JSON
http://bruceexpress.com/beerstore/test/getottawaeaststorebeerlist.php
使用PHP,我生成上述JSON从SQL查询和与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代码= 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 ???
在json中不应该有文档类型;你有没有正确设置标题(像这样:http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script)? – chrisjlee
你的PHP脚本还有什么回音/打印/输出? –
你正在打印'<!DOCTYPE html>'在你回应你的'JSON'之前将页面打印出来 – cmorrissey