note 92539 deleted from function.json-encode by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 12:15:18 +0000 |
| Subject: | note 92539 deleted from function.json-encode by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197102@lists.php.net to get a copy of this message | ||
Note Submitter: damon1977 at gmail dot com
----
I wrote a function to make JSON strings more readable. It's very useful for debugging JSON
output...
<?php
function jsonReadable($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;
}
?>