Prettify JSON Array in JavaScript
Possible Duplicate:
JSON pretty print using JavaScript
I'm working on a project that will be used to help analyse and understand JSON arrays by future developers of a platform. I'm referencing Facebook's brilliant Graph Explorer page, seen here, and want to output our array in a prettified, correctly tab indented and line breaker array, just as it does on the explorer.
The arrays are outputted to a textarea
, and because of this I think I'm running into issues with the line breaking and tabbing. I've also tried using the prettify library, but with no luck.
Example:
{"outcome" : "success", "result" : {"name" : "messaging-sockets", "default-interface" : "external", "include" : [], "socket-binding" : {"messaging" : {"name" : "messaging", "interface" : null, "port" : 5445, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}, "messaging-throughput" : {"name" : "messaging-throughput", "interface" : null, "port" : 5455, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}}}, "compensating-operation" : null}
To:
{
"outcome":"success",
"result":{
"name":"messaging-sockets",
"default-interface":"external",
"include":[
],
"socket-binding":{
"messaging":{
"name":"messaging",
"interface":null,
"port":5445,
"fixed-port":null,
"multicast-address":null,
"multicast-port":null
},
"messaging-throughput":{
"name":"messaging-throughput",
"interface":null,
"port":5455,
"fixed-port":null,
"multicast-address":null,
"multicast-port":null
}
}
},
"compensating-operation":null
}
You may use JSON.stringify:
JSON.stringify(jsonobj,null,'t')
See the demo.
UPDATE: If you don't have jsonobj
,but have json string,then before using stringify
function,convert json string to json object by this line:
jsonobj = JSON.parse(jsonstring);
试试这个解决方案,我也在我的项目中使用了这个
链接地址: http://www.djcxy.com/p/1320.html上一篇: 如何在Python中将字符串解析为float或int?
下一篇: 在JavaScript中美化JSON数组