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 ...
随机推荐
- WSGI剖析
在一个 HTTP 请求到达服务器时, 服务器接收并调用 web 应用程序解析请求, 产生响应数据并返回给服务器. 这里涉及了两个方面的东西: 服务器(server)和应用程序(application) ...
- 王彪-20162321《程序设计与数据结构2nd》-第十一周学习总结与实验报告
邻接矩阵实现 例图 分析 变量 需要一个链表来保存数据-即保存结点 需要一个二维数组来保存每个变得权值,有则填入具体数值,没有则用0 定义一个保存边个数的值 函数方法 得到图中边的个数 得到结点的数据 ...
- 1 Scala基本概念 +IDE
Scala基本概念 +IDE 推荐:<Scala编程> 1 基本概念 在Spark开发中,Scala被认为是目前和Spark兼容最好的语言. Scala运行在标准的java平台,可以与ja ...
- bzoj 1563
对于很多决策单调性DP问题,我们很难(但不是不可以)证明其决策满足单调性,所以感觉很像时,可以打表看是否满足. 这道题的精度(?范围)很难搞,开始生怕溢出,看了hzwer的代码,才发现用long do ...
- BZOJ 4197 NOI 2015 寿司晚宴 状压DP
4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 694 Solved: 440[Submit][Status] ...
- Codeforces Round #280 (Div. 2) A. Vanya and Cubes 水题
A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 索引式优先队列(indexed priority queue)
为了达到O(ElogV)的效率,需要对普利姆算法进行eager实现. 如果我们用java来做,jdk当中的priorityQueue并不能满足我们的要求. 因为我们需要进行一个对索引元素降key的操作 ...
- windows组策略和共享
Author: Jin Date: 20140585 ENV: win2008 R2 5年没弄windows了,现在随便弄弄,说实话不太喜欢windows,不出问题时候很方便,一出问题很头大.所有东西 ...
- Smali语法简单介绍
Smali语言其实就是Davlik的寄存器语言: Smali语言就是android的应用程序.apk通过apktool反编译出来的都有一个smali文件夹,里面都是以.smali结尾的文件,文件的展示 ...
- poj3322 Bloxorz I
Home Problems Logout -11:24:01 Overview Problem Status Rank A B C D E F G H I J K L M N O ...