原生js格式化json工具
json格式化小工具,原生js编写,直接上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>原生js格式化json的方法</title>
<script>
//格式化代码函数,已经用原生方式写好了不需要改动,直接引用就好
var formatJson = function (json) {
var formatted = '', //转换后的json字符串
padIdx = 0, //换行后是否增减PADDING的标识
PADDING = ' '; //4个空格符
/**
* 将对象转化为string
*/
if (typeof json !== 'string') {
json = JSON.stringify(json);
}
/**
*利用正则类似将{'name':'ccy','age':18,'info':['address':'wuhan','interest':'playCards']}
*---> \r\n{\r\n'name':'ccy',\r\n'age':18,\r\n
*'info':\r\n[\r\n'address':'wuhan',\r\n'interest':'playCards'\r\n]\r\n}\r\n
*/
json = json.replace(/([\{\}])/g, '\r\n$1\r\n')
.replace(/([\[\]])/g, '\r\n$1\r\n')
.replace(/(\,)/g, '$1\r\n')
.replace(/(\r\n\r\n)/g, '\r\n')
.replace(/\r\n\,/g, ',');
/**
* 根据split生成数据进行遍历,一行行判断是否增减PADDING
*/
(json.split('\r\n')).forEach(function (node, index) {
var indent = 0,
padding = '';
if (node.match(/\{$/) || node.match(/\[$/)) indent = 1;
else if (node.match(/\}/) || node.match(/\]/)) padIdx = padIdx !== 0 ? --padIdx : padIdx;
else indent = 0;
for (var i = 0; i < padIdx; i++) padding += PADDING;
formatted += padding + node + '\r\n';
padIdx += indent;
console.log('index:'+index+',indent:'+indent+',padIdx:'+padIdx+',node-->'+node);
});
return formatted;
};
//引用示例部分
//var originalJson = {'name':'ccy','age':18,'info':[{'address':'wuhan'},{'interest':'playCards'}]};
var showJson = function(){
var originalJson = document.getElementById('inputJson').value;
console.log(originalJson);
//(2)调用formatJson函数,将json格式进行格式化
var resultJson = formatJson(originalJson);
document.getElementById('out').innerHTML = resultJson;
}
</script>
</head>
<body>
<span style="position:absolute;left:0px;top:20px;font-size: 20px;font-family: '微软雅黑';color: #2F4F4F;">输入json</span>
<textarea style="position:absolute;left:0px;top:80px;width:40%;height:80%;" cols="50" rows="20" id="inputJson"></textarea>
<span style="position:absolute;left:55%;top:20px;font-size: 20px;font-family: '微软雅黑';color: #2F4F4F;">查看结果</span>
<textarea style="position:absolute;left:55%;top:80px;width:40%;height:80%;display: " id="out"></textarea>
<div style="position:absolute;left:45%;top:12%;width:6%;height:4%;">
<input type="button" value="提交" onclick="showJson();">
</div>
</body>
</html>
原生js格式化json工具的更多相关文章
- 原生js格式化json的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 原生js格式化json和格式化xml的方法
在工作中一直看各位前辈的博客解决各种问题,对我的帮助很大,非常感谢! 之前一直比较忙没有写博客,终于过年有了点空闲时间,可以把自己积累的东西分享下,笔记中的部分函数不是自己写的,都是工作中一点点积累的 ...
- 初探原生js根据json数据动态创建table
初探原生js根据json数据动态创建table 小生以实习生的职位进入了一家非纯软件的公司做asp.net开发,大半个月下来发现公司里居然没有前端工程师,这令我很诧异,跟着公司做项目,发现前端后台没有 ...
- js格式化JSON数据
前言: 最近做的项目中遇到个需要在前端页面中将某个设备需要的数据格式展示出来,方便用户配置.一开始单纯的将数据格式写入到pre标签中, 但是通过pre标签写入的数据格式在代码可视化上不是很优雅.由于上 ...
- js 格式化 json 字符串
1.JSON.stringify的三个参数 var json = {"@odata.context":"$metadata#AddTableOne_466281s&quo ...
- js格式化json字符串和json对象
1,格式化json对象 var json = {"@odata.context":"$metadata#AddTableOne_466281s","v ...
- 原生JS:JSON对象详解
JSON对象 支持到IE8,旧版的IE需要Polyfill 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/docs/Web ...
- js格式化日期工具类
就是一个工具类 开箱即用 传进一个指定的参数,格式化 //将时间戳格式化 function getMyDate(time){ if(typeof(time)=="undefined&quo ...
- JS格式化JSON后的日期
序列化后日期变成了 /Date(1494524134000+0800)\ 这种格式 不能正常显示了 但也不能为了这个吧所有服务的DateTime字段都改成String类型 于是找了一个JS的扩展方法来 ...
随机推荐
- APP微信支付报错《商户号该产品权限未开通,请前往商户平台>产品中心检查后重试》
问题 最近项目使用MUI,HBuilder.开发打包H5的app 在开发H5 plus支付的时候,遇到以下问题: App微信支付调官方的统一下单接口返回错误信息 {return_msg=商户号该产品权 ...
- PowerShell工作流学习-2-工作流运行Powershell命令
关键点: a)inlineScript 活动具有活动通用参数,但不具有PowerShell 通用参数,且inlineScript 脚本块中的命令和表达式不具有工作流的功能b)默认inlineScrip ...
- MFC头文件
AFX.H struct CRuntimeClass; // object type information class CObject; // the root of all objects cla ...
- 添加自己的component
在之前讲解esp-idf的文件结构时,曾经讲过component是esp-idf集成的功能块,这篇文章就来讲解下,如何在esp-idf 中添加自己的component. STEP1; 创建compon ...
- Struts2下载及简介
Struts2下载及简介 一.Struts2下载: 进入网址:http://struts.apache.org/download.cgi#struts23163 可以下载最新的Struts2. 1.F ...
- vue使用矢量图
1.在阿里巴巴矢量图标下载下来 2.放到static下 3.main.js下 import '../static/iconfont/iconfont.css' 4.调用<i class=&quo ...
- 4.BeanPostProcessor 后处理Bean
Bean种类 普通bean:之前操作的都是普通bean.<bean id="" class="A"> ,spring直接创建A实例,并返回 Fac ...
- Matlab实现《追光者》简谱
MATLAB除了生孩子,其他全都能做系列. 使用MATLAB进行播放<追光者>,纯文本内容哦. Fs = ; load y; sound(y, Fs); 加载的y是哪里来的呢?当然是调用函 ...
- Git入门基础详情教程
前言 写了一篇文章<一篇文章了解Github和Git教程>还觉得不错,继续写了<为了Github默默付出,我想了解你>,那么继续写Git 基础知识. Git 官网:https: ...
- ELK实战(Springboot日志输出查找)
需求 把分布式系统,集群日志集中处理快速查询 搭建ELK并与springboot日志输出结合 搭建ELK 基于我前面的elasticsearch搭建博客文档docker-compose.yml基础上进 ...