我在iOS中使用Alamofire时遇到了麻烦,因为我的php似乎没有返回有效的json。我添加了标题“header(”Content-Type:application / json; charset = utf-8“);”在编码数组之前,它仍然无法正常工作。如果重要,我的数据库表中的某些列是使用utf8mb4_unicode_ci编码的。 这是我的php:
<?php
$con = mysqli_connect("localhost","hide","hide","hide");
mysqli_set_charset($con, "utf8");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM Business";
if($result = mysqli_query($con, $sql))
{
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object())
{
$tempArray = $row;
array_push($resultArray, $tempArray);
}
header("Content-Type: application/json; charset=utf-8");
echo json_encode($resultArray);
}
mysqli_close($result);
mysqli_close($con);
?>
未格式化的json:
[{"business_id":"0","imageURL":"imageUrl","shortDescription":"shortDescription","longDescription":"longDescription","price":"price","specialText":"specialText","order_rank":"1"}]
这是格式化的json指定错误: