php 如何以编程方式美化 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6672656/
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
How can I beautify JSON programmatically?
提问by genesis
You might say this is duplicate of this question, but original question WASN'T answered there. Important part of question is: programmatically
?
你可能会说这是这个问题的重复,但原始问题没有在那里回答。问题的重要部分是:programmatically
?
Is there any php function? Native or homemade?
有没有php函数?国产的还是自制的?
采纳答案by GregSchoen
Looks like this might work:
看起来这可能有效:
http://recursive-design.com/blog/2008/03/11/format-json-with-php/
http://recursive-design.com/blog/2008/03/11/format-json-with-php/
genesis's result:
创世纪的结果:
[[["er",null,null,null,null,500],["e",2,null,null,57]],'45932b7d6d6dc08e']
to
到
[
[
[
"er",
null,
null,
null,
null,
500
],
[
"e",
2,
null,
null,
57
]
],
'45932b7d6d6dc08e'
]
回答by Mārti?? Briedis
json_encode()has a flag JSON_PRETTY_PRINT
json_encode()有一个标志JSON_PRETTY_PRINT
echo json_encode($data, JSON_PRETTY_PRINT);
回答by Hermann Bier
had the same question right now. But as you also i'm having php < 5.4. Zend Framework has Zend_Json::prettyPrint(). Works very well.
现在有同样的问题。但正如你一样,我的 php < 5.4。Zend 框架有 Zend_Json::prettyPrint()。效果很好。
回答by Edi Budimilic
This simple trick did the job for me, I didn't wanted any additional libraries or functions:
这个简单的技巧为我完成了这项工作,我不想要任何额外的库或函数:
$json = '{"status":"0","status_translated":"Request successful!","data":"1"}';
$json_beautified = str_replace(array("{", "}", '","'), array("{<br /> ", "<br />}", '",<br /> "'), $json);
And the result looks like this:
结果如下所示:
{
"status":"0",
"status_translated":"Request successful!",
"data":"1"
}
This is only for json code that goes 1 step in depth, I hope it helps.
这仅适用于深入 1 步的 json 代码,希望对您有所帮助。
回答by Juan Lago
I created a non-destructive JSON beautifier that support multiple deep levels.
我创建了一个支持多个深度级别的非破坏性 JSON 美化器。
/**
* JSON beautifier
*
* @param string The original JSON string
* @param string Return string
* @param string Tab string
* @return string
*/
function pretty_json($json, $ret= "\n", $ind="\t") {
$beauty_json = '';
$quote_state = FALSE;
$level = 0;
$json_length = strlen($json);
for ($i = 0; $i < $json_length; $i++)
{
$pre = '';
$suf = '';
switch ($json[$i])
{
case '"':
$quote_state = !$quote_state;
break;
case '[':
$level++;
break;
case ']':
$level--;
$pre = $ret;
$pre .= str_repeat($ind, $level);
break;
case '{':
if ($i - 1 >= 0 && $json[$i - 1] != ',')
{
$pre = $ret;
$pre .= str_repeat($ind, $level);
}
$level++;
$suf = $ret;
$suf .= str_repeat($ind, $level);
break;
case ':':
$suf = ' ';
break;
case ',':
if (!$quote_state)
{
$suf = $ret;
$suf .= str_repeat($ind, $level);
}
break;
case '}':
$level--;
case ']':
$pre = $ret;
$pre .= str_repeat($ind, $level);
break;
}
$beauty_json .= $pre.$json[$i].$suf;
}
return $beauty_json;
}
回答by vpathak
For command line usage, you can use the js beautifier. No need to share your data with external sites.
对于命令行使用,您可以使用 js 美化器。无需与外部站点共享您的数据。