php 格式 echo json_encode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15066976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:29:32  来源:igfitidea点击:

Format echo json_encode

phpjson

提问by Craig Weston

I would like to format the echo json_encode, the output is currently

我想格式化 echo json_encode,输出是当前

{"results":{"course":"CC140","books":{"book":[[{"id":"300862","title":"Building object-oriented software","isbn":"0070431965","borrowedcount":"6"}]]}}}

Whereas i would like to to output like this:

而我想像这样输出:

{
    "results": {
        "course": "CC140",
        "books": {
            "book": [
                [
                    {
                        "id": "300862",
                        "title": "Building object-oriented software",
                        "isbn": "0070431965",
                        "borrowedcount": "6"
                    }
                ]
            ]
        }
    }
}

This is the code that makes the JSON

这是生成 JSON 的代码

$temp = array();
    foreach ($my_array as $counter => $bc) {
        $temp['id'] = "$id[$counter]";
        $temp['title'] = "$title[$counter]";
        $temp['isbn'] = "$isbn[$counter]";
        $temp['borrowedcount'] = "$borrowedcount[$counter]";
        $t2[] = $temp;
    }

        $data = array(
  "results" => array(
    "course" => "$cc",
    "books" => array(
      "book" =>
      array(  
        $t2
      )
    )
  )
);
    echo json_encode($data);

Any help or pointers would be appreciated, thanks

任何帮助或指示将不胜感激,谢谢

Adding this

添加这个

header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);

formats the JSON, but the header also outs the entire HTML document

格式化 JSON,但标题也超出整个 HTML 文档

回答by Quentin

The first piece of advice I'd give is: Don't. JSON is a data format. Deal with it using tools rather then trying to have your server format it.

我给出的第一条建议是:不要。JSON 是一种数据格式。使用工具处理它,而不是尝试让您的服务器对其进行格式化。

If you are going to ignore that, then see the manual for the json_encodefunctionwhere it gives a list of optionswhich includes JSON_PRETTY_PRINTwhich is described as Use whitespace in returned data to format it. Available since PHP 5.4.0.

如果您要忽略它,请参阅该json_encode函数的手册,其中提供了一个选项列表,其中包括JSON_PRETTY_PRINT被描述为在返回的数据中使用空格对其进行格式化的选项。自 PHP 5.4.0 起可用。

Thus the steps are:

因此步骤是:

  1. Make sure you are using PHP 5.4.0 or newer
  2. json_encode($data, JSON_PRETTY_PRINT);
  1. 确保您使用的是 PHP 5.4.0 或更新版本
  2. json_encode($data, JSON_PRETTY_PRINT);

回答by pozs

You can use json_encode($data, JSON_PRETTY_PRINT)in php 5.4+

您可以json_encode($data, JSON_PRETTY_PRINT)在 php 5.4+ 中使用

In php 5.3 & under that, you could try formatting it with regular expressions, but it's not too safe (or you could use library for encoding json).

在 php 5.3 & 下,您可以尝试使用正则表达式对其进行格式化,但这不太安全(或者您可以使用库来编码 json)。