The examples in this article describe how PHP custom functions format JSON data. Share to everyone for your reference, specific as follows:
<?php/** * Formats A JSON string for pretty printing * * @param string $json The JSON-make pretty * @param bool $html Insert nonbreaking spaces and <br/>s for tabs and linebreaks * @return string The prettified o Utput */$arr = array ("ret" =>0, "Data" =>array (' a ' => 1, ' B ' => "cloud-Habitat community", ' C ' => 3, ' d ' => 4, ' E ' => 5
));
$json = Json_encode ($arr);
function _format_json ($json, $html = False) {$tabcount = 0;
$result = ';
$inquote = false;
$ignorenext = false;
if ($html) {$tab = " ";
$newline = "<br/>";
else {$tab = "\ t";
$newline = "\ n";
for ($i = 0; $i < strlen ($json); $i + +) {$char = $json [$i];
if ($ignorenext) {$result. = $char;
$ignorenext = false;
else {switch ($char) {case ' {': $tabcount + +; $result. = $char. $newline.
Str_repeat ($tab, $tabcount);
Break Case '} ': $tabcount-; $result = Trim ($result). $newline. Str_repeat ($tab, $tabcount).
$char;
Break Case ', ': $result. = $char. $newline.
Str_repeat ($tab, $tabcount);
Break
Case ' ": $inquote =! $inquote;
$result. = $char;
Break
Case ' \ ': if ($inquote) $ignorenext = true;
$result. = $char;
Break
Default: $result. = $char;
}} return $result;
echo _format_json ($json);
/* {"ret": 0, "data": {"a": 1, "B": "\u811a\u672c\u4e4b\u5bb6", "C": 3, "D": 4, "E": 5}} **/
?>
PS: Here again for you to recommend a few more practical JSON online tools for your reference to use:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json
For more information about PHP interested readers can view the site topics: "PHP JSON format data Operation tips Summary", "PHP for XML file Operation skills Summary", "PHP basic Grammar Introductory Course", "PHP Array" Operation Techniques Encyclopedia, "PHP string ( String) Usage summary, "Getting Started with Php+mysql database operations" and "Summary of common PHP database Operations Tips"
I hope this article will help you with the PHP program design.