I know at one point he was using Var Dumpling. That's what I use to format JSON at least.
Sep 11, 2015
4
Level 1
json test outputs properly formated
I'm doing 'my first app' series and I see Jeffrey usually returns some json outcome to test something before even building views however I noticed in the screen cast the his outcomes in the browser are properly formated while mine are a mess ;) how can I get them prettier?
Level 25
Level 13
In my own JSON output library I use this to clean up my JSON output. Warning, if the JSON coming in is bad it will BURP. So I wrap it an exception.
/*
* From GitHub Gist - by - GloryFish, with enhancement by roktir. For tabs.
* Nice work! Saved some time.
* Need to move this JSON writer and upcoming XML writer out of this class.
*/
static public function format_json($json, $html = false, $tabspaces = null)
{
$tabcount = 0;
$result = '';
$inquote = false;
$ignorenext = false;
if ($html) {
$tab = str_repeat(" ", ($tabspaces == null ? 4 : $tabspaces));
$newline = "<br/>";
} else {
$tab = ($tabspaces == null ? "\t" : str_repeat(" ", $tabspaces));
$newline = "\n";
}
for ($i = 0; $i < strlen($json); $i++) {
$char = $json[$i];
if ($ignorenext) {
$result .= $char;
$ignorenext = false;
} else {
switch ($char) {
case ':':
$result .= $char . (!$inquote ? " " : "");
break;
case '{':
if (!$inquote) {
$tabcount++;
$result .= $char . $newline . str_repeat($tab, $tabcount);
} else {
$result .= $char;
}
break;
case '}':
if (!$inquote) {
$tabcount--;
if ($tabcount < 0) {
throw new JSONFormatException("Parse Error: $result");
}
$result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char;
} else {
$result .= $char;
}
break;
case ',':
if (!$inquote) {
$result .= $char . $newline . str_repeat($tab, $tabcount);
} else {
$result .= $char;
}
break;
case '"':
$inquote = !$inquote;
$result .= $char;
break;
case '\\':
if ($inquote) {
$ignorenext = true;
}
$result .= $char;
break;
default:
$result .= $char;
}
}
}
return $result;
}
Change JSONExceptioin class.
Level 1
Thank you @thepsion5
That extension don't have a really good rating and didn't work but your answer really helped to find something else like https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh
Please or to participate in this conversation.