为什么我的php没有返回正确的json? (“期待UTF-8,UTF-16或UTF-32”)

时间:2016-06-21 14:02:18

标签: php json encoding

我在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指定错误:

enter image description here

0 个答案:

没有答案