在JavaScript中美化JSON数组 [英] Prettify JSON Array in JavaScript
问题描述
可能存在重复:
使用JavaScript的JSON漂亮打印
我正在处理这个项目将用于帮助未来的平台开发人员分析和理解JSON数组。我参考了Facebook辉煌的Graph Explorer页面,在这里看到,并且想要输出我们的数组正如它在资源管理器中所做的那样。
数组输出到 textarea code>,正因为如此,我认为我遇到了换行和换行的问题。
示例:
{outcome:success,result:{name:messaging-sockets,default-interface:external,include:[],绑定:{messaging:{name:messaging,interface:null,port:5445,fixed-port:null,multicast-address:null, :null,messaging-throughput:{name:messaging-throughput,interface:null,port:5455,fixed-port:null,multicast-address:null,多播端口:null}}},补偿操作:null}
/ b>
{
结果:成功,
结果:{
name:messaging-sockets,
default-interface:external,
include:[
],
套接字绑定:{
messaging:{
name:messaging,
interface:null,
port:5 445,
固定端口:空,
多播地址:空,
多播端口:空
},
吞吐量:{
name:messaging-throughput,
interface:null,
port:5455,
fixed-port
multicast-address:null,
multicast-port:null
}
}
},
补偿操作:null
您可以使用 JSON.stringify :
JSON.stringify(jsonobj,null,'\ t')
请参阅演示。
更新:如果你没有 jsonobj
,但是有json字符串,那么在你之前
stringify
函数,将json字符串转换为json对象: > jsonobj = JSON.parse(jsonstring);
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);
这篇关于在JavaScript中美化JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!