tags:

views:

121

answers:

1

I'm working with JSON by PHP at the moment, when I encode it, it would output as:

{"username":"ND","email":"[email protected]","regdate":"8th June 2010","other":{"alternative":"ND"},"level":"6"}

When I would like it to output like this:

{
    "username": "ND",
    "email": "[email protected]",
    "regdate": "8th June 2010",
    "other":
    {
        "alternative": "ND"
    },
    "level":"6"
}

So that me and my other developers can read it well enough when structured. How can I do this?

Example also like this:

https://graph.facebook.com/19292868552

Cheers

+2  A: 

I could find this useful myself, so here's a little function I wrote:

<?php
function json_pretty_encode($obj)
{
    $json = json_encode($obj);
    if (!$json) return $json;

    $f = '';
    $len = strlen($json);

    $depth = 0;
    $newline = false;

    for ($i = 0; $i < $len; ++$i)
    {
        if ($newline)
        {
            $f .= "\n";
            $f .= str_repeat(' ', $depth);
            $newline = false;
        }

        $c = $json[$i];
        if ($c == '{' || $c == '[')
        {
            $f .= $c;
            $depth++;
            $newline = true;
        }
        else if ($c == '}' || $c == ']')
        {
            $depth--;
            $f .= "\n";
            $f .= str_repeat(' ', $depth);
            $f .= $c;
        }
        else if ($c == '"')
        {
            $s = $i;
            do {
                $c = $json[++$i];
                if ($c == '\\')
                {
                    $i += 2;
                    $c = $json[$i];
                }
            } while ($c != '"');
            $f .= substr($json, $s, $i-$s+1);
        }
        else if ($c == ':')
        {
            $f .= ': ';
        }       
        else if ($c == ',')
        {
            $f .= ',';
            $newline = true;
        }
        else
        {
            $f .= $c;
        }
    }

    return $f;
}
?>

It's naive, trusting that PHP will return a valid JSON string. It could be written more concisely, but it's easy to modify this way. (And of course this adds unnecessary overhead in production scenarios where only a machine reads the text.)

Edit: Added an else clause to catch numbers and other unknown chars.

konforce
If you think it is naive to trust that PHP will produce JSON when the built in JSON function is used … how can you trust PHP enough to use it at all?
David Dorward
@David, I just meant it as a warning not to pull that code out for arbitrary parsing of JSON, as it expects the format that PHP supplies.@Peter, This is an encoder. You need to pass it an object to convert into JSON.
konforce
@Konforce - Sorry, brain freeze. I was just thinking about json_decode.
Peter Ajtai
Are you gonna throw in bitmasking support? ;) http://php.net/manual/en/function.json-encode.php
Peter Ajtai