javascript格式化json显示实例分析

作者:肥佬 时间:2024-05-22 10:31:14 

本文实例讲述了javascript格式化json显示方法。分享给大家供大家参考。具体分析如下:

将json对象或者json字符串格式化方便在网页上限制


var formatJson = function(json, options) {
var reg = null,
formatted = '',
pad = 0,
PADDING = '';
//one can also use '\t' or a different number of spaces
// optional settings
options = options || {};
// remove newline where '{' or '[' follows ':'
options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
// use a space after a colon
options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;
// begin formatting...
if (typeof json !== 'string') {
// make sure we start with the JSON as a string
json = JSON.stringify(json);
} else {
// is already a string, so parse and re-stringify
//in order to remove extra whitespace
json = JSON.parse(json);
json = JSON.stringify(json);
}
// add newline before and after curly braces
reg = /([\{\}])/g;
json = json.replace(reg, '\r\n$1\r\n');
// add newline before and after square brackets
reg = /([\[\]])/g;
json = json.replace(reg, '\r\n$1\r\n');
// add newline after comma
reg = /(\,)/g;
json = json.replace(reg, '$1\r\n');
// remove multiple newlines
reg = /(\r\n\r\n)/g;
json = json.replace(reg, '\r\n');
// remove newlines before commas
reg = /\r\n\,/g;
json = json.replace(reg, ',');
// optional formatting...
if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
reg = /\:\r\n\{/g;
json = json.replace(reg, ':{');
reg = /\:\r\n\[/g;
json = json.replace(reg, ':[');
}
if (options.spaceAfterColon) {  
reg = /\:/g;
json = json.replace(reg, ': ');
}
$.each(json.split('\r\n'), function(index, node) {
var i = 0,
 indent = 0,
 padding = '';
if (node.match(/\{$/) || node.match(/\[$/)) {
 indent = 1;
} else if (node.match(/\}/) || node.match(/\]/)) {
 if (pad !== 0) {
 pad -= 1;
 }
} else {
 indent = 0;
}
for (i = 0; i < pad; i++) {
 padding += PADDING;
}
formatted += padding + node + '\r\n';
pad += indent;
});
return formatted;
};

关于json格式化感兴趣的朋友还可参考在线工具:

JSON代码工具

希望本文所述对大家的javascript程序设计有所帮助。

标签:javascript,json
0
投稿

猜你喜欢

  • MySQL之MHA高可用配置及故障切换实现详细部署步骤

    2024-01-27 15:14:21
  • asp如何编写一个加法器?

    2009-11-08 18:58:00
  • SQL Server手工插入标识列的方法

    2024-01-27 05:41:35
  • 如何对PHP程序中的常见漏洞进行攻击(下)

    2023-11-16 14:50:19
  • PyTorch中的CUDA的操作方法

    2022-02-24 18:54:41
  • python flask项目打包成docker镜像发布的过程

    2021-06-28 17:38:21
  • Pthon批量处理将pdb文件生成dssp文件

    2021-10-07 13:11:04
  • 详解inet_pton()和inet_ntop()函数

    2023-07-23 04:38:26
  • python 窃取摄像头照片的实现示例

    2021-12-27 19:16:11
  • MYSQL配置参数优化详解

    2024-01-21 08:17:44
  • Python容器类型转换的3种方法实例

    2022-06-03 13:32:32
  • Python特效之数字成像方法详解

    2022-09-12 13:16:22
  • django中静态文件配置static的方法

    2022-07-29 08:52:51
  • Python GUI和游戏开发从入门到实践

    2021-01-09 15:30:47
  • SQL 截取字符串应用代码

    2024-01-21 08:53:14
  • python Django里CSRF 对应策略详解

    2021-02-15 20:36:19
  • 10个php函数实用却不常见

    2023-11-05 01:40:43
  • vue中element-ui表格缩略图悬浮放大功能的实例代码

    2024-05-29 22:44:31
  • Oracle 安装和卸载问题收集(集合篇)第1/6页

    2024-01-22 02:31:33
  • Python实现批量修改图片格式和大小的方法【opencv库与PIL库】

    2021-10-08 09:04:29
  • asp之家 网络编程 m.aspxhome.com