我正在构建一个PHP脚本,将JSON数据提供给另一个脚本。我的脚本将数据构建到一个大型关联数组中,然后使用json_encode输出数据。下面是一个脚本示例:

$data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip');
header('Content-type: text/javascript');
echo json_encode($data);

上面的代码产生如下输出:

{"a":"apple","b":"banana","c":"catnip"}

如果你有少量的数据,这是很好的,但我更喜欢这样的东西:

{
    "a": "apple",
    "b": "banana",
    "c": "catnip"
}

有没有办法在PHP中做到这一点,而不需要丑陋的黑客?似乎Facebook的某个人发现了这一点。


当前回答

我也有同样的问题。

不管怎样,我只是在这里使用了json格式代码:

http://recursive-design.com/blog/2008/03/11/format-json-with-php/

能满足我的需要。

还有一个更加维护的版本:https://github.com/GerHobbelt/nicejson-php

其他回答

许多用户建议您使用

echo json_encode($results, JSON_PRETTY_PRINT);

这完全正确。但这还不够,浏览器需要了解数据的类型,您可以在将数据回显给用户之前指定头部。

header('Content-Type: application/json');

这将产生格式良好的输出。

或者,如果你喜欢扩展,你可以使用JSONView Chrome。

我从Composer: https://github.com/composer/composer/blob/master/src/Composer/Json/JsonFile.php和nicejson: https://github.com/GerHobbelt/nicejson-php/blob/master/nicejson.php中获取代码 Composer代码很好,因为它从5.3到5.4的更新很流畅,但它只编码对象,而nicejson需要json字符串,所以我合并了它们。代码可以用来格式化json字符串和/或编码对象,我目前在Drupal模块中使用它。

if (!defined('JSON_UNESCAPED_SLASHES'))
    define('JSON_UNESCAPED_SLASHES', 64);
if (!defined('JSON_PRETTY_PRINT'))
    define('JSON_PRETTY_PRINT', 128);
if (!defined('JSON_UNESCAPED_UNICODE'))
    define('JSON_UNESCAPED_UNICODE', 256);

function _json_encode($data, $options = 448)
{
    if (version_compare(PHP_VERSION, '5.4', '>='))
    {
        return json_encode($data, $options);
    }

    return _json_format(json_encode($data), $options);
}

function _pretty_print_json($json)
{
    return _json_format($json, JSON_PRETTY_PRINT);
}

function _json_format($json, $options = 448)
{
    $prettyPrint = (bool) ($options & JSON_PRETTY_PRINT);
    $unescapeUnicode = (bool) ($options & JSON_UNESCAPED_UNICODE);
    $unescapeSlashes = (bool) ($options & JSON_UNESCAPED_SLASHES);

    if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes)
    {
        return $json;
    }

    $result = '';
    $pos = 0;
    $strLen = strlen($json);
    $indentStr = ' ';
    $newLine = "\n";
    $outOfQuotes = true;
    $buffer = '';
    $noescape = true;

    for ($i = 0; $i < $strLen; $i++)
    {
        // Grab the next character in the string
        $char = substr($json, $i, 1);

        // Are we inside a quoted string?
        if ('"' === $char && $noescape)
        {
            $outOfQuotes = !$outOfQuotes;
        }

        if (!$outOfQuotes)
        {
            $buffer .= $char;
            $noescape = '\\' === $char ? !$noescape : true;
            continue;
        }
        elseif ('' !== $buffer)
        {
            if ($unescapeSlashes)
            {
                $buffer = str_replace('\\/', '/', $buffer);
            }

            if ($unescapeUnicode && function_exists('mb_convert_encoding'))
            {
                // http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
                $buffer = preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
                    function ($match)
                    {
                        return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
                    }, $buffer);
            } 

            $result .= $buffer . $char;
            $buffer = '';
            continue;
        }
        elseif(false !== strpos(" \t\r\n", $char))
        {
            continue;
        }

        if (':' === $char)
        {
            // Add a space after the : character
            $char .= ' ';
        }
        elseif (('}' === $char || ']' === $char))
        {
            $pos--;
            $prevChar = substr($json, $i - 1, 1);

            if ('{' !== $prevChar && '[' !== $prevChar)
            {
                // If this character is the end of an element,
                // output a new line and indent the next line
                $result .= $newLine;
                for ($j = 0; $j < $pos; $j++)
                {
                    $result .= $indentStr;
                }
            }
            else
            {
                // Collapse empty {} and []
                $result = rtrim($result) . "\n\n" . $indentStr;
            }
        }

        $result .= $char;

        // If the last character was the beginning of an element,
        // output a new line and indent the next line
        if (',' === $char || '{' === $char || '[' === $char)
        {
            $result .= $newLine;

            if ('{' === $char || '[' === $char)
            {
                $pos++;
            }

            for ($j = 0; $j < $pos; $j++)
            {
                $result .= $indentStr;
            }
        }
    }
    // If buffer not empty after formating we have an unclosed quote
    if (strlen($buffer) > 0)
    {
        //json is incorrectly formatted
        $result = false;
    }

    return $result;
}

如果你正在使用MVC

尝试在您的控制器中执行此操作

public function getLatestUsers() {
    header('Content-Type: application/json');
    echo $this->model->getLatestUsers(); // this returns json_encode($somedata, JSON_PRETTY_PRINT)
}

然后如果你调用/getLatestUsers,你会得到一个漂亮的JSON输出;)

我通常会用这些简单的语句。

如果你已经有一个JSON字符串,你可以简单地使用echo()和print_r()的组合。 不要忘记将print_r()的第二个参数传递给true,以便它返回值而不是打印它:

echo('<pre>' . print_r($json, true) . '</pre>');

或者你可以使用die(),这对调试很方便:

die('<pre>' . print_r($json, true) . '</pre>');

如果您有一个数组,您需要在之前将其转换为JSON字符串。 请确保使用JSON_PRETTY_PRINT标志设置json_encode()的第二个参数,这样您的JSON将被正确呈现:

echo('<pre>' . print_r(json_encode($array, JSON_PRETTY_PRINT), true) . '</pre>');

或者用于调试:

die('<pre>' . print_r(json_encode($array, JSON_PRETTY_PRINT), true) . '</pre>');

更容易提醒:输入128

128是常量“JSON_PRETTY_PRINT”的同义词。

(PHP文档网站)

json_encode($json,128);
//similar to
json_encode($json,JSON_PRETTY_PRINT );