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 ...
随机推荐
- 深入理解python中的select模块
简介 Python中的select模块专注于I/O多路复用,提供了select poll epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kque ...
- 【WIN10】我的第一個WIN10-UWP應用——古文觀止
已上架,下載地址:https://www.microsoft.com/store/apps/9nblggh6cc32 特點是:繁體豎排,隱藏/顯示標點符號. 截幾張圖來瞅瞅. 1.主界面 這張圖使用的 ...
- [BZOJ5287][HNOI2018]毒瘤(虚树DP)
暴力枚举非树边取值做DP可得75. 注意到每次枚举出一个容斥状态的时候,都要做大量重复操作. 建立虚树,预处理出虚树上两点间的转移系数.也可动态DP解决. 树上倍增.动态DP.虚树DP似乎是这种问题的 ...
- 利用Pastezort渗透win7
下载Pastezort git clone https://github.com/ZettaHack/PasteZort.git 给Pastezort文件夹提升权限 /root/PasteZort/ ...
- Java混剪音频
分享一个之前看过的程序,可以用来剪辑特定长度的音频,将它们混剪在一起,思路如下: 1.使用 FileInputStream 输入两个音频 2.使用 FileInputStream的skip(long ...
- BZOJ 1029: [JSOI2007]建筑抢修 优先队列
1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec Memory Limit: 162 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- centos7搭建.netcore运行环境
开发环境介绍 1.操作系统:Windows Server 2008 R2 Enterprise 2.IDE:VisualStudio2017 3..Net Core 2.0 SDK 本文假设你已经满足 ...
- MessasgePack:一个小巧高效的序列化方式
MessagePack是一种高效二进制序列化格式.可以在多种语言中进行快速数据交换,比如JSON格式等.它比Json更加小巧,更加高效,可以用于一些结构化数据存储 ,非常适合适用于消息总线,Memor ...
- Syncthing vs BitTorrent Sync
Syncthing 是一款跨平台的文件同步工具.即你在一台设备上创建.修改或删除文件,在其他设备上会同步执行相同的操作.Syncthing 不会将你的数据上传到云端,而是在你的多台设备同时在线时对指定 ...
- Send custom commands to Mass Storage device
http://stackoverflow.com/questions/14363152/send-custom-commands-to-mass-storage-device I have devel ...