我试图获得与pythons json.dumps()相同的结果,并启用按键排序。这是Postman生成请求哈希的预请求脚本。输出需要对有效json进行排序,json用作散列的输入。我是javascript的新手,看到许多旧答案声称javascript中的对象无法排序。但是,必须有一个解决方案来生成给定条件的哈希。
这是我目前的代码。在Chrome控制台中,sortedResult的对象预览未排序,但是当我展开对象和子对象时,Chrome控制台将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);
示例输入:
{
"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"
}
在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"
}
答案 0 :(得分:1)
一种常见的误解是&#34;对象键没有被排序&#34;在javascript中。 MDN states
虽然ECMAScript使对象的迭代顺序依赖于实现,但似乎所有主流浏览器都支持基于最先添加的属性的迭代顺序(至少对于不在原型上的属性)。
和ES2015 makes this behaviour standard:
对于每个自己的属性键P,O是一个String但不是整数索引,在属性创建顺序中......
也就是说,您可以依赖于对象属性始终按插入顺序迭代的事实(除非您使用delete
,有关详细信息,请参阅here)。
因此,要对某个对象中的键进行排序,只需创建一个新对象并按排序顺序为其添加键:
sortKeys = function(x) {
if(typeof x !== 'object')
return x;
if(Array.isArray(x))
return x.map(sortKeys);
var res = {};
Object.keys(x).sort().forEach(k => res[k] = sortKeys(x[k]));
return res;
}
////
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)
document.write('<pre>'+JSON.stringify(sorted,0,3));
&#13;