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. WSGI剖析

    在一个 HTTP 请求到达服务器时, 服务器接收并调用 web 应用程序解析请求, 产生响应数据并返回给服务器. 这里涉及了两个方面的东西: 服务器(server)和应用程序(application) ...

  2. 王彪-20162321《程序设计与数据结构2nd》-第十一周学习总结与实验报告

    邻接矩阵实现 例图 分析 变量 需要一个链表来保存数据-即保存结点 需要一个二维数组来保存每个变得权值,有则填入具体数值,没有则用0 定义一个保存边个数的值 函数方法 得到图中边的个数 得到结点的数据 ...

  3. 1 Scala基本概念 +IDE

    Scala基本概念 +IDE 推荐:<Scala编程> 1 基本概念 在Spark开发中,Scala被认为是目前和Spark兼容最好的语言. Scala运行在标准的java平台,可以与ja ...

  4. bzoj 1563

    对于很多决策单调性DP问题,我们很难(但不是不可以)证明其决策满足单调性,所以感觉很像时,可以打表看是否满足. 这道题的精度(?范围)很难搞,开始生怕溢出,看了hzwer的代码,才发现用long do ...

  5. BZOJ 4197 NOI 2015 寿司晚宴 状压DP

    4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 694  Solved: 440[Submit][Status] ...

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

  7. 索引式优先队列(indexed priority queue)

    为了达到O(ElogV)的效率,需要对普利姆算法进行eager实现. 如果我们用java来做,jdk当中的priorityQueue并不能满足我们的要求. 因为我们需要进行一个对索引元素降key的操作 ...

  8. windows组策略和共享

    Author: Jin Date: 20140585 ENV: win2008 R2 5年没弄windows了,现在随便弄弄,说实话不太喜欢windows,不出问题时候很方便,一出问题很头大.所有东西 ...

  9. Smali语法简单介绍

    Smali语言其实就是Davlik的寄存器语言: Smali语言就是android的应用程序.apk通过apktool反编译出来的都有一个smali文件夹,里面都是以.smali结尾的文件,文件的展示 ...

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