META-INF.resources.opendoc.format.js Maven / Gradle / Ivy
/**
* 格式化json字符串.
* alert(formatJson('{"code":"0","data":[{"goods_name":"iPhoneX","id":1,"price":8000},{"goods_name":"三星","id":2,"price":7000}]}'))
* @param txt json串
* @param compress
* @returns
*/
function formatJson(txt,compress/*是否为压缩模式*/){/* 格式化JSON源码(对象转换为JSON文本) */
var indentChar = ' ';
if(/^\s*$/.test(txt)){
alert('数据为空,无法格式化! ');
return;
}
try{var data=eval('('+txt+')');}
catch(e){
alert('数据源语法错误,格式化失败! 错误信息: '+e.description,'err');
return;
};
var draw=[],last=false,This=this,line=compress?'':'\n',nodeCount=0,maxDepth=0;
var notify=function(name,value,isLast,indent/*缩进*/,formObj){
nodeCount++;/*节点计数*/
for (var i=0,tab='';i