function removeEmptyField(obj) { var newObj = {}; if(typeof obj == "string"){ obj = JSON.parse(obj); } if(obj instanceof Array){ newObj = []; } if(obj instanceof Object){ for(var attr in obj){ if(obj.hasOwnProperty(attr) && obj[attr] !== "" && obj[attr] !== null && obj[attr] !== undefined){ if(obj[attr] instanceof Object){ newObj[attr] = removeEmptyField(obj[attr]); }else if(typeof obj[attr] == "string" && ((obj[attr].indexOf("{") > -1 && obj[attr].indexOf("}") > -1) || (obj[attr].indexOf("[") > -1 && obj[attr].indexOf("]") > -1))){ try{ var attrObj = JSON.parse(obj[attr]); if(attrObj instanceof Object){ newObj[attr] = removeEmptyField(attrObj); } }catch (e){ newObj[attr] = obj[attr]; } }else{ newObj[attr] = obj[attr]; } } } } return newObj; }
对象转字符串去除无效字段
function stringifyNoEmptyField(obj){ var newObj = removeEmptyField(obj); for(var attr in newObj){ if(newObj.hasOwnProperty(attr) && newObj[attr] instanceof Array){ newObj[attr] = JSON.stringify(newObj[attr]); } } return JSON.stringify(newObj); }
时间: 2024-10-04 11:54:06