I’m working with JSON by PHP at the moment, when I encode it, it would output as:
{"username":"ND","email":"test@email.com","regdate":"8th June 2010","other":{"alternative":"ND"},"level":"6"}
When I would like it to output like this:
{
"username": "ND",
"email": "test@email.com",
"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:
Cheers
I could find this useful myself, so here’s a little function I wrote:
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.