Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 人类可读的JSON:aka向JSON转储添加空格和分隔符_Php_Json_Var Dump - Fatal编程技术网

Php 人类可读的JSON:aka向JSON转储添加空格和分隔符

Php 人类可读的JSON:aka向JSON转储添加空格和分隔符,php,json,var-dump,Php,Json,Var Dump,是否有一个“简单”的脚本可以接收json数据并对其进行格式化以使其更具可读性 例如: // $response is a json encoded string. var_dump($response); 上面的输出都在一条线上。我希望它能够缩进并隔开,以便更容易阅读。注意,var\u dump及其更简洁的表亲可以打印换行符 请记住,默认情况下,HTML文档中不会显示换行符。在HTML上下文中,您希望改为: echo '<div style="font-family: monospace

是否有一个“简单”的脚本可以接收json数据并对其进行格式化以使其更具可读性

例如:

// $response is a json encoded string.
var_dump($response);

上面的输出都在一条线上。我希望它能够缩进并隔开,以便更容易阅读。

注意,
var\u dump
及其更简洁的表亲可以打印换行符

请记住,默认情况下,HTML文档中不会显示换行符。在HTML上下文中,您希望改为:

echo '<div style="font-family: monospace; white-space:pre;">';
echo htmlspecialchars(var_export($response));
echo '</div>';
echo '<pre>';
print_r(json_decode($response));
echo '</pre>';

同样,在HTML上下文中,必须如上所述对其进行包装。

将其粘贴到中,然后单击“验证”

通过管道将其传送到python-mjson.tool

有一个类似的问题,我将一个序列化的javascript对象发布到php脚本中,并希望以人类可读的格式保存到服务器上

找到并稍微调整了代码以适应我自己的感觉(需要一个json编码的字符串):

返回

{
    "object": {
        "array": [
            "one",
            "two"
        ],
        "sub-object": {
            "one": "string",
            "two": 2
        }
    }
}

使用python的建议对我很有效。下面是一些使用PHP的代码:

function jsonEncode( $data, $pretty = false ) {
    $str = json_encode($data);
    if( $pretty ) {
        $descriptorSpec = array(
                0 => array('pipe', 'r'),  // stdin is a pipe that the child will read from
                1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
        );
        $fp = proc_open('/usr/bin/python -mjson.tool', $descriptorSpec, $pipes);
        fputs($pipes[0], $str);
        fclose($pipes[0]);
        $str = '';
        while( !feof($pipes[1]) ) {
            $str .= $chunk = fgets($pipes[1], 1024);
        }
        fclose($pipes[1]);
    }
    return $str;
}
echo';
打印(json解码($response));
回声';
太简单了?

json\u编码($response,json\u PRETTY\u PRINT)

现在是2017年,我认为这应该是任何使用现代版PHP的人的答案

请注意,对于如何根据自己的喜好对JSON字符串进行编码,存在很多选项。发件人:
JSON\u HEX QUOT、JSON\u HEX\u标记、JSON\u HEX\u AMP、JSON\u HEX\u APOS、JSON\u数字检查、JSON\u漂亮打印、JSON\u未缩放斜杠、JSON\u强制\u对象、JSON\u保留零分数、JSON\u未缩放\u UNICODE、JSON\u部分输出\u错误

@Matt Anderson谢谢,添加了一条注释,提到需要更新的php,并添加了
var\u export
。很酷的技巧,但我目前使用的是5.3。。。我以后会记住的。这似乎是最快最简单的选择。。。有用的小工具。谢谢我的首选是这个站点:也就是说,JavaScript有一个很好的JSON解析函数来执行这个函数。如果这只是为了阅读JSON,那么像JSONView(,)这样的浏览器插件会有用吗?(还有很多其他类似的插件。)我非常喜欢它,我把它转换成了Java。享受发现这个的人。
{
    "object": {
        "array": [
            "one",
            "two"
        ],
        "sub-object": {
            "one": "string",
            "two": 2
        }
    }
}
function jsonEncode( $data, $pretty = false ) {
    $str = json_encode($data);
    if( $pretty ) {
        $descriptorSpec = array(
                0 => array('pipe', 'r'),  // stdin is a pipe that the child will read from
                1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
        );
        $fp = proc_open('/usr/bin/python -mjson.tool', $descriptorSpec, $pipes);
        fputs($pipes[0], $str);
        fclose($pipes[0]);
        $str = '';
        while( !feof($pipes[1]) ) {
            $str .= $chunk = fgets($pipes[1], 1024);
        }
        fclose($pipes[1]);
    }
    return $str;
}
echo '<pre>';
print_r(json_decode($response));
echo '</pre>';