EmEditor自定义宏——json递归排序
测试日志时需要对比json,需要将json格式化排序,emeditor的宏使用的语言是jscript5.8版本(ie的javascript),遵循ES3标准,只能使用早期的JavaScript 1.5代码,所以需要加一些垫片,后文附宏源文件代码。
·
目录
IE8对JSON.stringify中文转换成unicode的问题
背景
在测试日志的时候需要对比json数据,于是想到了将json格式化排序后做对比
调研
现在市面上能够自定义宏的文本编辑器不多,notepad++的宏只能录制回放不能自定义编辑所以排除,emeditor的宏可以录制,一开始使用的网上查到的ES6版本的javascript代码,修改后并测试通过,然而emeditor的宏使用的语言是jscript5.8版本(ie的javascript),遵循ES3标准,只能使用早期的JavaScript 1.5代码,所以需要加一些垫片(朋友告诉我的),后文附宏源文件jsee。
资料查阅
IE8对JSON.stringify中文转换成unicode的问题
JS将unicode码转中文方法(解决IE8对JSON.stringify中文转换成unicode的问题)_wulongren520的博客-CSDN博客
ECMAScript标准
「每日一题」ES 3、ES 5、ES 6 分别是什么 - 知乎
EmEditor帮助截图

实现
测试数据
{"boolean":true,"array":[3,1,2],"null":null,"number":123,"object":{"c":"d","a":"b"},"string":"Hello World","xobjarray":[{"c":"d","a":"b"},{"g":"你","e":"好"}]}
排序后数据
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d"
},
"string": "Hello World",
"xobjarray": [
{
"a": "b",
"c": "d"
},
{
"e": "好",
"g": "你"
}
]
}
json递归排序.jsee
if (!Object.keys) {
Object.keys = function(obj) {
var keys = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
keys.push(i);
}
}
return keys;
};
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function forEach(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError("this is null or not defined");
}
var O = Object(this);
var len = O.length >>> 0;
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
if (arguments.length > 1) {
T = thisArg;
}
k = 0;
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}
if ('function' !== typeof Array.prototype.reduce) {
Array.prototype.reduce = function(callback, opt_initialValue) {
'use strict';
if (null === this || 'undefined' === typeof this) {
// At the moment all modern browsers, that support strict mode, have
// native implementation of Array.prototype.reduce. For instance, IE8
// does not support strict mode, so this check is actually useless.
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if ('function' !== typeof callback) {
throw new TypeError(callback + ' is not a function');
}
var index = 0,
length = this.length >>> 0,
value, isValueSet = false;
if (1 < arguments.length) {
value = opt_initialValue;
isValueSet = true;
}
for (; length > index; ++index) {
if (!this.hasOwnProperty(index)) continue;
if (isValueSet) {
value = callback(value, this[index], index, this);
} else {
value = this[index];
isValueSet = true;
}
}
if (!isValueSet) {
throw new TypeError('Reduce of empty array with no initial value');
}
return value;
};
}
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
if (!Array.prototype.map) {
Array.prototype.map = function(callback, thisArg) {
var T, A, k;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
var O = Object(this);
var len = O.length >>> 0;
if (Object.prototype.toString.call(callback) != "[object Function]") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
A = new Array(len);
k = 0;
while (k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[k];
mappedValue = callback.call(T, kValue, k, O);
A[k] = mappedValue;
}
k++;
}
return A;
};
}
function sortJSON(obj) {
// 如果是数组,则递归每个元素
if (Array.isArray(obj)) {
if (typeof obj[0] === 'object') {
return obj.map(sortJSON);
} else {
return obj.sort();
}
}
// 如果是对象,则递归每个属性
else if (typeof obj === 'object' && obj != null) {
return Object.keys(obj).sort().reduce(function(acc, key) {
acc[key] = sortJSON(obj[key]);
return acc;
},
{});
}
// 如果是其他类型,则直接返回
else {
return obj;
}
}
document.selection.SelectAll();
document.selection.Copy(eeCopyUnicode);
var jsonString = clipboardData.getData("Text");
// 将 JSON 字符串解析为 JavaScript 对象
var jsonStr = jsonString;
var jsonObj = JSON.parse(jsonStr);
var sortedObj = sortJSON(jsonObj);
var sortedJsonStr = JSON.stringify(sortedObj, null, 4);
// ie8将中文转成了unicode,需要将unicode再转成中文
clipboardData.setData("Text", unescape(sortedJsonStr.replace(/\\u/g, '%u')));
document.selection.SelectAll();
document.selection.Paste(eeCopyUnicode);
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)