更好地可视化json字符串

时间:2014-03-06 20:30:54

标签: php json

我将表数据作为json响应。但回应看起来搞砸了。

[{"0":"8","sno":"8","1":"chennai","city":"chennai","2":"active","status":"active"},{"0":"9","sno":"9","1":"kolkatta","city":"kolkatta","2":"active","status":"active"},{"0":"10","sno":"10","1":"bangalore","city":"bangalore","2":"active","status":"active"},{"0":"11","sno":"11","1":"ahmedabad","city":"ahmedabad","2":"active","status":"active"},{"0":"12","sno":"12","1":"hyderabad","city":"hyderabad","2":"active","status":"active"},{"0":"13","sno":"13","1":"delhi","city":"delhi","2":"active","status":"active"}]

可以更好地展示吗?

php代码:

function getcity($getcity)
{
    $con = mysqli_connect('127.0.0.1', 'root', '', 'safari');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        return;
    }   
    $today = date("Ymd");           
    $result1 = array();
    $result = mysqli_query($con,"SELECT sno,city,status from tbl_demo");
    while ($row = @mysqli_fetch_array($result))
    {
        array_push($result1,$row);
    }
    echo  $result1 = json_encode($result1,true);  

}

是否可以这样显示:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

2 个答案:

答案 0 :(得分:1)

mysqli_fetch_array()返回一个包含数字和字符串键的数组,因此您可以获得两次数据(一次使用数字索引,一次使用字符串索引)。尝试:

$row = mysqli_fetch_assoc($result);

或者:

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

如果你实际上是在谈论它的“外观”,那么请查看PHP数组。为什么JSON需要以某种方式“看”?

<pre>
<?php print_r($result1); ?>
</pre>

或者:

<pre>
<?php var_export($result1); ?>
</pre>

答案 1 :(得分:0)

为您的浏览器安装JSON插件。

您正在使用json_encode对其进行编码,因此您可能只想在浏览器中以您能够理解的方式查看它。