Last active
December 17, 2015 22:59
-
-
Save weishai/5686003 to your computer and use it in GitHub Desktop.
简单的js代码可以实现格式化json,使用方法很简单: 网页换行和制表符不一样,设置一下 JsonUti.n = "<br />"; JsonUti.t = " "; document.getElementById("afterformat").innerHTML = JsonUti.convertToString(json); 需要注意,此处的json是JSON类型,如果是string的话需要先把string转成JSON格式。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var JsonUti = { | |
//定义换行符 | |
n: "\n", | |
//定义制表符 | |
t: "\t", | |
//转换String | |
convertToString: function(obj) { | |
return JsonUti.__writeObj(obj, 1); | |
}, | |
//写对象 | |
__writeObj: function(obj //对象 | |
, level //层次(基数为1) | |
, isInArray) { //此对象是否在一个集合内 | |
//如果为空,直接输出null | |
if (obj == null) { | |
return "null"; | |
} | |
//为普通类型,直接输出值 | |
if (obj.constructor == Number || obj.constructor == Date || obj.constructor == String || obj.constructor == Boolean) { | |
var v = obj.toString(); | |
var tab = isInArray ? JsonUti.__repeatStr(JsonUti.t, level - 1) : ""; | |
if (obj.constructor == String || obj.constructor == Date) { | |
//时间格式化只是单纯输出字符串,而不是Date对象 | |
return tab + ("\"" + v + "\""); | |
} | |
else if (obj.constructor == Boolean) { | |
return tab + v.toLowerCase(); | |
} | |
else { | |
return tab + (v); | |
} | |
} | |
//写Json对象,缓存字符串 | |
var currentObjStrings = []; | |
//遍历属性 | |
for (var name in obj) { | |
var temp = []; | |
//格式化Tab | |
var paddingTab = JsonUti.__repeatStr(JsonUti.t, level); | |
temp.push(paddingTab); | |
//写出属性名 | |
temp.push(name + " : "); | |
var val = obj[name]; | |
if (val == null) { | |
temp.push("null"); | |
} | |
else { | |
var c = val.constructor; | |
if (c == Array) { //如果为集合,循环内部对象 | |
temp.push(JsonUti.n + paddingTab + "[" + JsonUti.n); | |
var levelUp = level + 2; //层级+2 | |
var tempArrValue = []; //集合元素相关字符串缓存片段 | |
for (var i = 0; i < val.length; i++) { | |
//递归写对象 | |
tempArrValue.push(JsonUti.__writeObj(val[i], levelUp, true)); | |
} | |
temp.push(tempArrValue.join("," + JsonUti.n)); | |
temp.push(JsonUti.n + paddingTab + "]"); | |
} | |
else if (c == Function) { | |
temp.push("[Function]"); | |
} | |
else { | |
//递归写对象 | |
temp.push(JsonUti.__writeObj(val, level + 1)); | |
} | |
} | |
//加入当前对象“属性”字符串 | |
currentObjStrings.push(temp.join("")); | |
} | |
return (level > 1 && !isInArray ? JsonUti.n : "") //如果Json对象是内部,就要换行格式化 | |
+ JsonUti.__repeatStr(JsonUti.t, level - 1) + "{" + JsonUti.n //加层次Tab格式化 | |
+ currentObjStrings.join("," + JsonUti.n) //串联所有属性值 | |
+ JsonUti.n + JsonUti.__repeatStr(JsonUti.t, level - 1) + "}"; //封闭对象 | |
}, | |
__isArray: function(obj) { | |
if (obj) { | |
return obj.constructor == Array; | |
} | |
return false; | |
}, | |
__repeatStr: function(str, times) { | |
var newStr = []; | |
if (times > 0) { | |
for (var i = 0; i < times; i++) { | |
newStr.push(str); | |
} | |
} | |
return newStr.join(""); | |
} | |
} | |
//example | |
var jsonText = JsonUti.convertToString(jsonData); //jsonData必须为json对象 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment