dongyan9838
2017-10-04 10:51
浏览 73

我可以使用<br>标签改进“json_encode”结果的可读性吗? [重复]

This question already has an answer here:

i was just echoing my statement results as JSON-Object. But the function json_encode() puts out everything into a single line like that:

{"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}

Now imagine a JSONobject with hundred of more entries.

Is there a way to add some <br> tags or tab stops to improve the readabilty? this should only be for test purposes not for further parsing.

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 使用PHP漂亮打印JSON 22 answers < / li>

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

      {“0”:{“username”:“Olli”},“1”:{  “用户名”: “海纳”}, “2”:{ “用户名”: “罗尔夫”}, “成功”:真, “行数”:3 “0”:{ “用户名”: “奥利”},“1  “:{” 用户名 “:” 海纳 “},” 2 “:{” 用户名 “:” 罗尔夫 “},” 成功 “:真,” 行数 “:3” 0 “:{” 用户名 “:” 奥利“} 中, “1”:{ “用户名”: “海纳”}, “2”:{ “用户名”: “罗尔夫”}, “成功”:真, “行数”:3 “0”:{ “用户名”:” 奥利 “},” 1 “:{” 用户名 “:” 海纳 “},” 2 “:{” 用户名 “:” 罗尔夫 “},” 成功 “:真,” 行数 “:3” 0 “:{” 用户名 “:” 奥利 “},” 1 “:{” 用户名 “:” 海纳 “},” 2 “:{” 用户名 “:” 罗尔夫 “},” 成功 “:真,” 行数 “:3” 0" :  { “用户名”: “奥利”}, “1”:{ “用户名”: “海纳”}, “2”:{ “用户名”: “罗尔夫”}, “成功”:真, “行数”:3"  0 “:{” 用户名 “:” 奥利 “},” 1 “:{” 用户名 “:” 海纳 “},” 2 “:{” 用户名 “:” 罗尔夫 “},” 成功 “:真,” 行数”  :3 “0”:{ “用户名”: “奥利”}, “1”:{ “用户名”: “海纳”}, “2”:{ “用户名”: “罗尔夫”}, “成功”:真,  “行数”:3 “0”:{ “用户名”: “奥利”}, “1”:{ “用户名”: “海纳”}, “2”:{ “用户名”: “罗尔夫”}, “成功”  :真正的“RO  wcount “:3” 0 “:{” 用户名 “:” 奥利 “},” 1 “:{” 用户名 “:” 海纳 “},” 2 “:{” 用户名 “:” 罗尔夫 “},” 成功“: 真, “行数”:3 “0”:{ “用户名”: “奥利”}, “1”:{ “用户名”: “海纳”}, “2”:{ “用户名”: “罗尔夫”},”  success“:true,”rowcount“:3} 
       
     
     

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

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douluokuang7184 2017-10-04 10:53
    已采纳

    Checkout the JSON_PRETTY_PRINT flag.

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

    Outputs :

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

    Ref : http://php.net/json_encode

    打赏 评论

相关推荐 更多相似问题