1. // Example usage: http://jsfiddle.net/q2gnX/
  2. var formatJson = function(json, options) {
  3. var reg = null,
  4. formatted = '',
  5. pad = 0,
  6. PADDING = ' '; // one can also use '\t' or a different number of spaces
  7. // optional settings
  8. options = options || {};
  9. // remove newline where '{' or '[' follows ':'
  10. options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
  11. // use a space after a colon
  12. options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;
  13. // begin formatting...
  14. if (typeof json !== 'string') {
  15. // make sure we start with the JSON as a string
  16. json = JSON.stringify(json);
  17. } else {
  18. // is already a string, so parse and re-stringify in order to remove extra whitespace
  19. json = JSON.parse(json);
  20. json = JSON.stringify(json);
  21. }
  22. // add newline before and after curly braces
  23. reg = /([\{\}])/g;
  24. json = json.replace(reg, '\r\n$1\r\n');
  25. // add newline before and after square brackets
  26. reg = /([\[\]])/g;
  27. json = json.replace(reg, '\r\n$1\r\n');
  28. // add newline after comma
  29. reg = /(\,)/g;
  30. json = json.replace(reg, '$1\r\n');
  31. // remove multiple newlines
  32. reg = /(\r\n\r\n)/g;
  33. json = json.replace(reg, '\r\n');
  34. // remove newlines before commas
  35. reg = /\r\n\,/g;
  36. json = json.replace(reg, ',');
  37. // optional formatting...
  38. if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
  39. reg = /\:\r\n\{/g;
  40. json = json.replace(reg, ':{');
  41. reg = /\:\r\n\[/g;
  42. json = json.replace(reg, ':[');
  43. }
  44. if (options.spaceAfterColon) {
  45. reg = /\:/g;
  46. json = json.replace(reg, ':');
  47. }
  48. $.each(json.split('\r\n'), function(index, node) {
  49. var i = 0,
  50. indent = 0,
  51. padding = '';
  52. if (node.match(/\{$/) || node.match(/\[$/)) {
  53. indent = 1;
  54. } else if (node.match(/\}/) || node.match(/\]/)) {
  55. if (pad !== 0) {
  56. pad -= 1;
  57. }
  58. } else {
  59. indent = 0;
  60. }
  61. for (i = 0; i < pad; i++) {
  62. padding += PADDING;
  63. }
  64. formatted += padding + node + '\r\n';
  65. pad += indent;
  66. });
  67. return formatted;
  68. };

使用方法:
  1. var json={"name":"HTL","sex":"男","age":"24"};
  2. console.log(formatJson(json));


javascript格式化json显示的更多相关文章

  1. js/javascript format json(js/javascript 格式化json字符串)

    // format json obj string function format_json(txt, compress) { var indentChar = '    '; if (/^\s*$/ ...

  2. Javascript格式化json返回的时间(/Date(1482747413000)/)

    //时间处理,类似/Date(1482747413000)/的形式,得到xxx年xx月xx日 11:11:11 function ChangeDateFormat(jsondate) { jsonda ...

  3. C#后台格式化JSON字符串显示

    很多时候我们从服务器上获取的JSON字符串是没有格式化的,如下: {"BusinessId":null,"Code":200,"Data": ...

  4. VIM中格式化json

    在vim输入以下命令就可以格式化:%!python -m json.tool可以在~/.vimrc增加快捷键map <F4><Esc>:%!python -m json.too ...

  5. AngularJS学习--- 过滤器(filter),格式化要显示的数据 step 9

    1.切换目录,启动项目 git checkout step- npm start 2.需求: 格式化要显示的数据. 比如要将true-->yes,false-->no,这样相互替换. 3. ...

  6. 小白科普之JavaScript的JSON

    一.对json的理解     json是一种数据格式,不是一种编程语言,json并不从属于javascript.     json的语法可以表示以下三种类型的值     1)简单值           ...

  7. C#格式化JSON字符串

    很多时候我们需要将json字符串以 {     "status": 1,     "sum": 9 }这种方式显示,而从服务端取回来的时候往往是这样 {&quo ...

  8. 简单使用JSON,JavaScript读取JSON文本(三)

    JavaScript 读取 JSON 文本转换为对象 JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 Jav ...

  9. javaScript系列 [05]-javaScript和JSON

    本文输出和JSON有关的以下内容❐ JSON和javaScript❐ JSON的语法介绍❐ JSON的数据类型❐ JSON和XMLHTTPRequest❐ JSON的序列化和反序列化处理 1.1 JS ...

随机推荐

  1. py脚本打包exe可执行文件

    python3以上版本打包exe需要扩展软件:cx_freeze 下载地址:http://cx-freeze.sourceforge.net/ 1)安装后在\Python32\Scripts\cxfr ...

  2. 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 ...

  3. 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 ...

  4. Bootstrap_CSS概览

    在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...

  5. jquery 图片预加载

    /图片无序预加载 (function($){ function Preload(imgs,fns){ this.imgs=(typeof imgs==="string")?[img ...

  6. Java中常见的IO流及其使用

    Java中IO流分成两大类,一种是输入流.全部的输入流都直接或间接继承自InputStream抽象类,输入流作为数据的来源.我们能够通过输入流的read方法读取字节数据.还有一种是输出流,全部的输出流 ...

  7. How to match between physical usb device and its drive letter?

    struct tagDrives { WCHAR letter; WCHAR volume[ BUFFER_SIZE ]; } g_drives[ ]; // WCHAR GetUSBDrive( ) ...

  8. ORA-00918:未明确定义列

    <script type="text/javascript"><!-- google_ad_client = "pub-9528830580198364 ...

  9. 使用Enum.TryParse()实现枚举的安全转换

    在项目中,有时候会用到领域枚举和DTO枚举的映射和转换.有一个现实的问题是:如果领域枚举项发生变化,而DTO枚举项没有及时更新,这样会造成映射不上的问题.那么,如何避免此类问题呢? 先看领域枚举和DT ...

  10. datagrid在MVC中的运用10-勾选

    本文体验与勾选有关的特性. 需要加载的books.json 展开{ "total": 4, "rows": [ { "productid": ...