首页 > 解决方案 > JSON 输出为空

问题描述

我有以下 PHP 代码来返回一些 JSON 编码的结果。如果我双重编码(即在我将当前行添加到数组的位置放置一个 json_encode),我会得到一个过度处理(到处都有很多“/”)但完整的输出。因为它在这里,我什么也得不到。

$link = mysqli_connect("localhost","steph_stephen", "haltermann1!", "stephenh_tanks");
if (!$link){
    die("Conection failed: " . mysqli_connect_error());
}
$query="SELECT * FROM Tank";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0){
    $myArray = array();
    header('Content-type:application/json');
    while($tank = mysqli_fetch_assoc($result))
    {
        //$myArray[] = json_encode($tank); if i leave this line in (and comment the line below) - i get the full but over-process json result
        $myArray[] = $tank; //without the line above no json string is returned
    }
    print json_encode($myArray);
}
else
{
    echo "Could not execute query";
    echo mysqli_error($link);
}
mysqli_close($link);

下面是返回的 JSON 的一小部分(尽管处理过度)

["{\"tank\":\"001\",\"product\":\"Caromax 20HN B\\\/F\",\"prod_code\":\"51533\",\"tank_level\":\"7888\",\"volume\":\"1325818\",\"pump_volume\":\"1275282\",\"max_volume\":\"1759741\",\"tank_status\":\"For Test\"}"...

标签: php

解决方案


推荐阅读