邮递员响应以不同的顺序响应来自请求的控制台日志响应 [英] Postman response in different order to console log response from request
问题描述
当使用 Postman 测试 API 请求时,我得到的响应是无序的,这正是我想要的,即:
When using Postman to test an API a request, the response i get is unordered, which is what I want, i.e.:
}
"23": "Kevin",
"2": "James",
"12": "Michael"
}
但是,当我记录响应时,它会自动将其排序为:
However, when I log the response, it automatically sorts it to:
}
"2": "James",
"12": "Michael",
"23": "Kevin"
}
我收到了承诺中的回复:
I am getting the response in a promise:
return this.client.post('/url', data)
.then(response => {
console.log('api response: ', response);
无论如何我可以阻止响应的排序,以便我可以根据需要呈现数据?我不确定数据的顺序在哪里或为什么不同.
Is there anyway I can prevent the ordering of the response so I can render the data as desired? I am not sure where, or why the order of the data is different.
任何帮助将不胜感激!
推荐答案
您不能也不应该依赖 JSON 对象中元素的顺序.
You cannot and should not rely on the ordering of elements within a JSON object.
对象是一组无序的名称/值对
An object is an unordered set of name/value pairs
因此,JSON 库可以按照他们认为合适的方式自由地重新排列元素的顺序.这不是错误.
As a consequence, JSON libraries are free to rearrange the order of the elements as they see fit. This is not a bug.
这篇关于邮递员响应以不同的顺序响应来自请求的控制台日志响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!