我正在做一个AJAX请求以从API获取一些响应,例如Twitter的API,并使用highlight.js向用户显示响应以突出显示代码,但通常服务器响应的空格更少/新尽可能划线,如下:
[{"in_reply_to_user_id_str":null,"contributors":null,"coordinates":null,"favorited":false,"id_str":"235118001274368000","geo":null,"user":{"id_str":"481202814","profile_background_tile":false,"id":481202814,"time_zone":"Brasilia","screen_name":"Juu_kimura","profile_sidebar_fill_color":"F6FFD1","default_profile_image":false,"location":"Paran\u00e1 ","favourites_count":2 ...
有没有办法对此进行格式化,以便在应用高亮显示之前更加人性化?
PS:响应可以是任何格式,JSON,XML,HTML,......
答案 0 :(得分:1)
如果您已将response
解析为JavaScript对象:
var response = {"in_reply_to_user_id_str":null,"contributors":null,"coordinates":null,"favorited":false,"id_str":"235118001274368000"};
JSON.stringify(response, null, 4);
的产率:
"{
"in_reply_to_user_id_str": null,
"contributors": null,
"coordinates": null,
"favorited": false,
"id_str": "235118001274368000"
}"
我不知道XML和HTML的任何通用解决方案。