javascript格式化json显示
// Example usage: http://jsfiddle.net/q2gnX/var formatJson = function(json, options) {var reg = null,formatted = '',pad = 0,PADDING = ' '; // one can also use '\t' or a different number of spaces// optional settingsoptions = options || {};// remove newline where '{' or '[' follows ':'options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;// use a space after a colonoptions.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;// begin formatting...if (typeof json !== 'string') {// make sure we start with the JSON as a stringjson = JSON.stringify(json);} else {// is already a string, so parse and re-stringify in order to remove extra whitespacejson = JSON.parse(json);json = JSON.stringify(json);}// add newline before and after curly bracesreg = /([\{\}])/g;json = json.replace(reg, '\r\n$1\r\n');// add newline before and after square bracketsreg = /([\[\]])/g;json = json.replace(reg, '\r\n$1\r\n');// add newline after commareg = /(\,)/g;json = json.replace(reg, '$1\r\n');// remove multiple newlinesreg = /(\r\n\r\n)/g;json = json.replace(reg, '\r\n');// remove newlines before commasreg = /\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;};
var json={"name":"HTL","sex":"男","age":"24"};console.log(formatJson(json));

javascript格式化json显示的更多相关文章
- js/javascript format json(js/javascript 格式化json字符串)
// format json obj string function format_json(txt, compress) { var indentChar = ' '; if (/^\s*$/ ...
- Javascript格式化json返回的时间(/Date(1482747413000)/)
//时间处理,类似/Date(1482747413000)/的形式,得到xxx年xx月xx日 11:11:11 function ChangeDateFormat(jsondate) { jsonda ...
- C#后台格式化JSON字符串显示
很多时候我们从服务器上获取的JSON字符串是没有格式化的,如下: {"BusinessId":null,"Code":200,"Data": ...
- VIM中格式化json
在vim输入以下命令就可以格式化:%!python -m json.tool可以在~/.vimrc增加快捷键map <F4><Esc>:%!python -m json.too ...
- AngularJS学习--- 过滤器(filter),格式化要显示的数据 step 9
1.切换目录,启动项目 git checkout step- npm start 2.需求: 格式化要显示的数据. 比如要将true-->yes,false-->no,这样相互替换. 3. ...
- 小白科普之JavaScript的JSON
一.对json的理解 json是一种数据格式,不是一种编程语言,json并不从属于javascript. json的语法可以表示以下三种类型的值 1)简单值 ...
- C#格式化JSON字符串
很多时候我们需要将json字符串以 { "status": 1, "sum": 9 }这种方式显示,而从服务端取回来的时候往往是这样 {&quo ...
- 简单使用JSON,JavaScript读取JSON文本(三)
JavaScript 读取 JSON 文本转换为对象 JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 Jav ...
- javaScript系列 [05]-javaScript和JSON
本文输出和JSON有关的以下内容❐ JSON和javaScript❐ JSON的语法介绍❐ JSON的数据类型❐ JSON和XMLHTTPRequest❐ JSON的序列化和反序列化处理 1.1 JS ...
随机推荐
- py脚本打包exe可执行文件
python3以上版本打包exe需要扩展软件:cx_freeze 下载地址:http://cx-freeze.sourceforge.net/ 1)安装后在\Python32\Scripts\cxfr ...
- HDU 1692 Destroy the Well of Life 水题
Destroy the Well of Life Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- Codeforces Round #283 (Div. 2) A. Minimum Difficulty 暴力水题
A. Minimum Difficulty time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Bootstrap_CSS概览
在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...
- jquery 图片预加载
/图片无序预加载 (function($){ function Preload(imgs,fns){ this.imgs=(typeof imgs==="string")?[img ...
- Java中常见的IO流及其使用
Java中IO流分成两大类,一种是输入流.全部的输入流都直接或间接继承自InputStream抽象类,输入流作为数据的来源.我们能够通过输入流的read方法读取字节数据.还有一种是输出流,全部的输出流 ...
- How to match between physical usb device and its drive letter?
struct tagDrives { WCHAR letter; WCHAR volume[ BUFFER_SIZE ]; } g_drives[ ]; // WCHAR GetUSBDrive( ) ...
- ORA-00918:未明确定义列
<script type="text/javascript"><!-- google_ad_client = "pub-9528830580198364 ...
- 使用Enum.TryParse()实现枚举的安全转换
在项目中,有时候会用到领域枚举和DTO枚举的映射和转换.有一个现实的问题是:如果领域枚举项发生变化,而DTO枚举项没有及时更新,这样会造成映射不上的问题.那么,如何避免此类问题呢? 先看领域枚举和DT ...
- datagrid在MVC中的运用10-勾选
本文体验与勾选有关的特性. 需要加载的books.json 展开{ "total": 4, "rows": [ { "productid": ...