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. 用profile协助程序性能优化

     程序运行慢的原因有很多,比如存在太多的劣化代码(如在程序中存在大量的“.”操作符),但真正的原因往往是比较是一两段设计并不那么良好的不起眼的程序,比如对一序列元素进行自定义的类型转换等.因为程序性能 ...

  2. 【干货】PHP常见危险函数

    passthru() 功能描述:允许执行一个外部程序并回显输出,类似于 exec(). 危险等级:高 exec() 功能描述:允许执行一个外部程序(如 UNIX Shell 或 CMD 命令等). 危 ...

  3. [IOI2007]Miners

    [IOI2007]Miners 题目大意: 两个人吃东西,总共有\(3\)种食物,每个人每次吃到食物时可以获得的收益是当前食物和前两次吃的食物中,不同食物的种数.现在给定一个长度为\(n(n\le10 ...

  4. IDEA JSP项目构建及学习心得

    近期学习的东西比较杂乱,导致了很多东西都有些忘却.在这里记录一份心得. 简而言之JSP也就是Java代码在页面上的一种呈现方式,用于Web项目的前台展示. 在这里不做过多的阐述. MVC设计模式,Se ...

  5. servlet生命周期和执行流程

    一 .生命周期 servlet 声明周期可以分四个阶段: 类装载过程 init() 初始化过程 service() 服务过程,选择doGet \ doPost destroy() 销毁过程 servl ...

  6. [LearnOpenGL]照相机的变换、坐标系、摄像机

    前言 跟着LearnOpenGL上学着做项目,的确对于知识掌握得更清晰一些了. 第一个项目 第一个项目,是关于简单的熟悉矩阵变换的,创建了10个立方体,代码如下. // 视图矩阵,看作是一个照相机 g ...

  7. python IDLE 自动提示功能

    \Python27\Lib\idlelib\目录下 config-extensions.def文件修改等待时间 [AutoComplete] enable=1 popupwait=2000(2000表 ...

  8. opencv第二课 进行缩放图片~

    #include<stdio.h> #include<iostream> #include<opencv2\opencv.hpp> using namespace ...

  9. Caffe2(2)----Eclipse环境中使用Caffe2

    使用IDE开发深度学习的应用,可以事半功倍,Caffe2已经全面支持Python,这里介绍如何在Ubantu14.04下,利用EclipseCaffe2的二次开发或应用. 1.安装eclipse 具体 ...

  10. Run native executable in Android App

    Run native executable in Android App Demo † Here's demo application called "Run Native Exe" ...