PHP数组不使用json_encode转换为JSON

时间:2017-05-02 15:10:32

标签: php mysql json

我正在尝试将我的$response数组转换为JSON,但它没有转换。我试图回显数组中的值,以确保它不是null。

    if($row['count(*)']>0)
    {
        $sql="select * from guide_city_detail where CITY='$place'";
        $result = $conn->query($sql);
        $row = $result1->fetch_assoc();
        $row = array_map('utf8_encode', $row);
        $response['status']=1;
        $response['city']=$row['CITY'];
        $response['state']=$row['STATE'];
        $response['country']=$row['COUNTRY'];
        $response['heads']=$row['HEADS'];
        $response['col1']=$row['COL1'];
        $response['col2']=$row['COL2'];
        $response['col3']=$row['COL3'];
        $response['col4']=$row['COL4'];
        $response['col5']=$row['COL5'];
        $response['col6']=$row['COL6'];
        $response['col7']=$row['COL7'];
        $response['col8']=$row['COL8'];
        $response['col9']=$row['COL9'];
        //echo $response['country'];
    }
    else
    {
        $response['status']=0;// save the entry for future references
        $output = shell_exec("java -cp \".;D:/letsgo/mysql.jar;D:/letsgo/jsoup.jar;\" GeneralPopulate $place 2>&1");
        //echo "$output";
    }
    $res=json_encode($response);
    echo $res;
}

1 个答案:

答案 0 :(得分:0)

如果json_last_error为您提供JSON_ERROR_UTF8错误代码(5),那么您必须使用UTF-8对数据进行编码:

需要将最终数组转换为utf-8。

<?php

$response = array_map('utf8_encode', $response);