我想得到和Python一样的结果json.dumps文件()启用了排序键。这是作为Postman生成请求散列的请求前脚本来实现的。输出需要对作为哈希输入的有效json进行排序。我对javascript不熟悉,看到很多老答案声称javascript中的对象不能排序。但是必须有一个解决方案来生成给定条件的散列。在
这是我当前的代码。在Chrome控制台中,sortedResult的对象预览是未排序的,但是当我展开对象和子对象时,Chrome控制台将sortedResult显示为sortedResult,完全是它应该显示的方式。这给我的印象是sortObject正在工作。然而,requestOrdered返回有效的json对象,但它没有排序。我最初的想法是JSON.stringify()正在取消排序。在
const requestRebuilt = {"username": user, "password": password, "sTime": time, "function": function,
"functionParams": requestParams, "salt": salt};
function sortObject(object){
var keys = _.keys(object);
var sortedKeys = _.sortBy(keys, function(key){
//console.log(key);
return key;
});
var sortedObj = {};
var sortedObjJson = "";
for(var index in keys){
var key = keys[index];
//console.log(key + ' ' + typeof object[key]);
if(typeof object[key] == 'object' && !(object[key] instanceof Array)){
sortedObj[key] = sortObject(object[key]);
} else if(object[key] instanceof Array) {
//sortedObj[key] = object[key].sort();
var arrayLength = object[key].length;
for (var i = 0; i < arrayLength; i++) {
sortedObj[key] = sortObject(object[key][i]);
//console.log(object[key][i]);
}
} else {
sortedObj[key] = object[key];
}
}
return sortedObj;
}
const sortedResult = sortObject(requestRebuilt);
console.log(sortedResult);
const requestOrdered = JSON.stringify(sortedResult);
console.log(requestOrdered);
var hash = CryptoJS.SHA256(requestOrdered).toString();
postman.setGlobalVariable("hash", hash);
输入示例:
^{pr2}$在python中,这是通过以下方法实现的:
ordered = json.dumps(
{"username": user, "password": password, "time": time, "function": function, "functionParams": functionParams, "salt": salt}
sort_keys=True, separators=(',', ':'))
订购结果:
{"function":"begin","functionParams":{"passenger":[{"bans":{"count":2,"weight":9},"dateOfBirth":"1915-10-02T00:00:00Z","email":"jdoe@mail.com","firstName":"John","phone":1234567890}],"pad":4,"tip":"ABC123FFG"},"pep":"sdeODQ0T","salt":"00d878f5e203","sTime":"2016-03-04T13:53:37Z","username":"jdoe@mail.com"}
印刷精美,便于阅读,但实际结果不应包含空格或新行:
{
"function": "begin",
"functionParams": {
"passenger": [
{
"bans": {
"count": 2,
"weight": 9
},
"dateOfBirth": "1915-10-02T00:00:00Z",
"email": "jdoe@mail.com",
"firstName": "John",
"phone": 1234567890
}
],
"pad": 4,
"tip": "ABC123FFG"
},
"pep": "sdeODQ0T",
"salt": "00d878f5e203",
"sTime": "2016-03-04T13:53:37Z",
"username": "jdoe@mail.com"
}
在javascript中,一种常见的误解是“对象键没有排序”。MDN states那个
和ES2015 makes this behaviour standard:
也就是说,您可以依赖这样一个事实:对象属性总是按插入顺序迭代(除非您使用的是
delete
,有关详细信息,请参见here)。在因此,要对某个对象中的键进行排序,只需创建一个新对象并按排序顺序向其添加键:
function sortKeys(x) { if (typeof x !== 'object' || !x) return x; if (Array.isArray(x)) return x.map(sortKeys); return Object.keys(x).sort().reduce((o, k) => ({...o, [k]: sortKeys(x[k])}), {}); } //// obj = { "username": "jdoe@mail.com", "sTime": "2016-03-04T13:53:37Z", "function": "begin", "functionParams": { "tip": "ABC123FFG", "pad": 4, "passenger": [{ "firstName": "John", "phone": 1234567890, "email": "jdoe@mail.com", "dateOfBirth": "1915-10-02T00:00:00Z", "bans": { "weight": 9, "count": 2 } } ] }, "salt": "00d878f5e203", "pep": "sdeODQ0T" } sorted = sortKeys(obj); console.log(sorted);
;