工作中遇到需要前端通过javascript对后端返回的json或者xml进行格式化处理的问题,特此记录下来,仅供交流
1.对json字符串进行格式化处理:
var jsonObj = JSON.parse(后端返回的json字符串);
var result = JSON.stringify(jsonObj, null, 2);//格式化
2.对xml字符串进行格式化处理:
1)、定义格式化方法
String.prototype.removeLineEnd = function(){
return this.replace(/(<.+?\s+?)(?:\n\s*?(.+?=".*?"))/g,'$1 $2');
}
function formatXml(text){
//去掉多余的空格
text = '\n' + text.replace(/(<\w+)(\s.*?>)/g,
function($0, name, props){
return name + ' ' + props.replace(/\s+(\w+=)/g," $1");
}).replace(/>\s*?</g,">\n<");
//把注释编码
text = text.replace(/\n/g,'\r').replace(/<!--(.+?)-->/g,
function($0, text){
var ret = '<!--' + escape(text) + '-->';
return ret;
}).replace(/\r/g,'\n');
//调整格式
var rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
var nodeStack = [];
var output = text.replace(rgx,function($0,all,name,isBegin,isCloseFull1,isCloseFull2 ,isFull1,isFull2){
var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/');
var prefix = '';
if(isBegin == '!'){
prefix = getPrefix(nodeStack.length);
}else {
if(isBegin != '/'){
prefix = getPrefix(nodeStack.length);
if(!isClosed){
nodeStack.push(name);
}
}else{
nodeStack.pop();
prefix = getPrefix(nodeStack.length);
}
}
var ret = '\n' + prefix + all;
return ret;
});
var prefixSpace = -1;
var outputText = output.substring(1);
//把注释还原并解码,调格式
outputText = outputText.replace(/\n/g,'\r').replace(/(\s*)<!--(.+?)-->/g,
function($0, prefix, text){
if(prefix.charAt(0) == '\r') prefix = prefix.substring(1);
text = unescape(text).replace(/\r/g,'\n');
var ret = '\n' + prefix + '<!--' + text.replace(/^\s*/mg, prefix ) + '-->';
return ret;
});
return outputText.replace(/\s+$/g,'').replace(/\r/g,'\r\n');
}
function getPrefix(prefixIndex){
var span = ' ';
var output = [];
for(var i = 0 ; i < prefixIndex; ++i){
output.push(span);
}
return output.join('');
}
2)、调用方法进行格式化
var result = formatXml(后端返回的xml字符串);