For the syntax highlighting, use code prettify. I believe this is what StackOverflow uses for its code highlighting.
- Wrap your formatted JSON in code blocks and give them the "prettyprint" class.
- Include prettify.js in your page.
- Make sure your document's body tag calls
prettyPrint()
when it loads
You will have syntax highlighted JSON in the format you have laid out in your page. See here for an example. So if you had a code block like this:
<code class="prettyprint">
var jsonObj = {
"height" : 6.2,
"width" : 7.3,
"length" : 9.1,
"color" : {
"r" : 255,
"g" : 200,
"b" : 10
}
}
</code>
It would look like this:
var jsonObj = {
"height" : 6.2,
"width" : 7.3,
"length" : 9.1,
"color" : {
"r" : 255,
"g" : 200,
"b" : 10
}
}
This doesn't help with the indenting, but the other answers seem to be addressing that.