Created
April 21, 2011 10:39
-
-
Save hiromi2424/934136 to your computer and use it in GitHub Desktop.
simple json formatter(possible to use formatted)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// almost same output can be seen at: | |
// http://jsonformatter.curiousconcept.com/ | |
class JsonDebug { | |
function format($json, $output = false) { | |
$depth = 0; | |
$length = strlen($json); | |
$result = ''; | |
for ($n = 0; $n < $length; $n++) { | |
$c = $json{$n}; | |
if ($c === '}' || $c === ']') { | |
$depth--; | |
$result .= self::__break($depth); | |
} | |
$result .= $c; | |
if ($c === '{' || $c === '[') { | |
$depth++; | |
$result .= self::__break($depth); | |
} | |
if ($c === ',') { | |
$result .= self::__break($depth); | |
} | |
} | |
if ($output) { | |
echo $result; | |
} | |
return $result; | |
} | |
function __break($depth) { | |
return "\n" . str_repeat("\t", $depth); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have used this and created https://jsonformatter.org. thank you.