首页 > JavaScript原生代码处理JSON的一些高频次方法合集

JavaScript原生代码处理JSON的一些高频次方法合集

let json = {/**判断JSON格式*/ isJSON: function (str) {if (typeof str == "string") {try {var obj = JSON.parse(str);if (typeof obj == "object" && obj) {return true;} else {return false;}} catch (e) {return false;}} else if (typeof str == "object") {return Object.prototype.toString.call(obj).toLocaleLowerCase() == "[object object]" && !obj.length;}return false;}, 
/**合并string类型的json*/ mergeJsonString: function () {return JSON.parse((jsonStr1 + jsonStr2).replace(/}{/g, ","));}, /**拼接两个json*/ mergeJsonObject: function (json1, json2) {for (var i in json1) {json2[i] = json1[i];}return json2;}, 
/**将JSON对象转换为字符串*//** param 将要转为URL参数字符串的对象,key URL参数字符串的前缀, encode true/false 是否进行URL编码 默认为true return URL参数字符串,index不需要传参*/ toGetString: function (param, key, encode, index, firstStr='?') {index || (index = 0);index++;if (param == null) return;var ps = "";var t = typeof (param);if (t == "string" || t == "number" || t == "boolean") {ps += "&" + key + "=" + ((encode == null || encode) ? encodeURIComponent(param) : param);} else {for (var i in param) {var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);ps += json.toGetString(param[i], k, encode, index, firstStr);}}index == 1 && (ps = ps.replace("&", firstStr));return ps;/*测试用例*//* var obj = {a: '1', 'b': {c: '3'}, d: [{e: '4'}]}; alert(json.toGetString(obj)); alert(json.toGetString(obj, 'CLASS'));*/}, 
/**JSON转GET请求参数(只针对包含一个层级的json格式)*/ toGetStringForOneDepthLevel: function (json, firstStr='?') {return firstStr+JSON.stringify(json).replace(/	|
|
|"|{|}/g, "").replace(/,/g, "&").replace(/:/g, "=");}, 
/**GET请求参数转JSON(只针对包含一个层级的json格式)*/ fromGetStringForOneDepthLevel: function (getString) {return JSON.parse("{"" + getString.substr(1 + getString.indexOf("?")).replace(/&/g, "","").replace(/=/g, "":"") + ""}");}, 
/**格式化json字符串格式*/string: function (json) {return JSON.stringify(json, null, 4);}, 
/**获取json对象一级属性个数*/ length: function (obj) {var count = 0;for (var i in obj) {count++;}return count;},//比较两个json是否相同isObj(object) {return object && typeof (object) == "object" && Object.prototype.toString.call(object).toLowerCase() == "[object object]";},isArray(object) {return object && typeof (object) == "object" && object.constructor == Array;},getLength(object) {var count = 0;for (var i in object) count++;return count;},compare(objA, objB) {if (!this.isObj(objA) || !this.isObj(objB)) return false; //判断类型是否正确if (this.getLength(objA) != this.getLength(objB)) return false; //判断长度是否一致return this.compareObj(objA, objB, true);//默认为true},compareObj(objA, objB, flag) {for (var key in objA) {if (!flag) //跳出整个循环break;if (!objB.hasOwnProperty(key)) {flag = false;break;}if (!this.isArray(objA[key])) { //子级不是数组时,比较属性值if (objB[key] != objA[key]) {flag = false;break;}} else {if (!this.isArray(objB[key])) {flag = false;break;}var oA = objA[key], oB = objB[key];if (oA.length != oB.length) {flag = false;break;}for (var k in oA) {if (!flag) //这里跳出循环是为了不让递归继续break;flag = this.compareObj(oA[k], oB[k], flag);}}}return flag;}
};
//去掉值为null或undefined的对象属性
Object.keys(d).forEach(k=>d[k]==null&&delete d[k]);//去掉值为null或undefined或''的对象属性
Object.keys(d).forEach(k=>(d[k]==null||d[k]==='')&&delete d[k]);

更多相关: