我可以用<br/>标签改进“json_encode”结果的可读性吗?

时间:2017-10-04 10:51:22

标签: php json encode

我只是将我的语句结果作为JSON-Object回应。但是函数json_encode()将所有内容都放到一行中:

{"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3"0":{"username":"Olli"},"1":{"username":"Heiner"},"2":{"username":"Rolf"},"success":true,"rowcount":3}

现在设想一个包含数百个条目的JSON对象。

有没有办法添加一些<br>标签或制表位以提高可读性?这应仅用于测试目的,而不是用于进一步解析。

1 个答案:

答案 0 :(得分:4)

签出JSON_PRETTY_PRINT标志。

echo json_encode([[[[[[['a'=>'b']]]]]]],JSON_PRETTY_PRINT);

输出:

[
    [
        [
            [
                [
                    [
                        {
                            "a": "b"
                        }
                    ]
                ]
            ]
        ]
    ]
]

参考:http://php.net/json_encode