- 启动Firefox/Chrome/Safari
- Open Firebug/developer tools
- Copy/paste your code into the console.
然后输入console.log(JSON.stringify(object))
,瞧
- Copy/paste back into your text editor.
For more control over the formatting, I have a free online webpage:
that lets you paste JSON or JS values in one box and see JSON at the bottom, with lots of knobs and sliders to adjust how it looks. For example, the JS value ["foo","bar",{dogs:42,piggies:0,cats:7},{jimmy:[1,2,3,4,5],jammy:3.14159265358979,hot:"pajammy"}]
can be formatted like any of the following (and more):
[
"foo", <- adjustable indentation
"bar",
{"dogs":42,"piggies":0,"cats":7}, <- small objects on one line!
{
"jimmy":[1,2,3,4,5], <- small arrays on one line!
"jammy":3.142, <- decimal precision!
"hot":"pajammy"
}
]
[
"foo",
"bar",
{ "cats":7, "dogs":42, "piggies":0 }, <- spaces inside braces!
{
"hot":"pajammy", <- sort object keys!
"jammy":3.14159265358979,
"jimmy":[ 1, 2, 3, 4, 5 ] <- spaces after commas!
}
]
[ "foo", <- 'short' format puts first value
"bar", <- on same line as opening bracket...
{ "dogs" : 42,
"piggies" : 0,
"cats" : 7 }, <- ...and close bracket with last value!
{ "jimmy" : [ 1, 2, 3, 4, 5 ],
"jammy" : 3.14159265358979, <- spaces around colons!
"hot" : "pajammy" } ] <- align object values!