按字母顺序对 HTTP 请求的 JSON 主体进行排序
Sort the JSON Body of HTTP Request alphabetically
我正在使用某些参数进行 API 调用。请求的主体是这样的:
{
"billing": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com"
},
"address": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com",
"telephone": "+919999999999"
},
"payments": [
{
"code": "abcd",
"amount": 500
}
],
"refno": "abcd123",
"successUrl": "https://baseurl/ordercomplete/success",
"failureUrl": "https://baseurl/ordercomplete/failure",
"products": [
{
"sku": "sampleSKU",
"price": 500,
"qty": 1,
"currency": 356,
"giftMessage": "",
"theme": ""
}
],
"syncOnly": true,
"deliveryMode": "API"
}
我想按字母顺序对请求的参数进行排序。排序应该在外层和内层完成。例如,排序后 address
应该在 billing
之前。在内部 JSON 中,我也希望对其进行排序。例如在 billing
结构中 email
应该在 lastname
.
之前
所以我正在寻找的答案是:
{
"address": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com",
"telephone": "+919999999999"
},
"billing": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com"
},
"deliveryMode": "API",
"failureUrl": "https://baseurl/ordercomplete/failure",
"payments": [
{
"code": "abcd",
"amount": 500
}
],
"products": [
{
"sku": "sampleSKU",
"price": 500,
"qty": 1,
"currency": 356,
"giftMessage": "",
"theme": ""
}
],
"refno": "abcd123",
"successUrl": "https://baseurl/ordercomplete/success",
"syncOnly": true
}
我想我可以通过创建多个具有所有字段的 POJO class 然后实现一个按字母顺序排序的比较器来做到这一点。但是这种做法即使请求体的参数中的单个字段发生变化也会变得非常困难。
所以我在想我们能不能用一些更好的方法来做到这一点,而不必担心字段结构。
您可以使用 Jackson ObjectMapper 并将 ObjectMapper 配置为
om.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
希望有用。
我正在使用某些参数进行 API 调用。请求的主体是这样的:
{
"billing": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com"
},
"address": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com",
"telephone": "+919999999999"
},
"payments": [
{
"code": "abcd",
"amount": 500
}
],
"refno": "abcd123",
"successUrl": "https://baseurl/ordercomplete/success",
"failureUrl": "https://baseurl/ordercomplete/failure",
"products": [
{
"sku": "sampleSKU",
"price": 500,
"qty": 1,
"currency": 356,
"giftMessage": "",
"theme": ""
}
],
"syncOnly": true,
"deliveryMode": "API"
}
我想按字母顺序对请求的参数进行排序。排序应该在外层和内层完成。例如,排序后 address
应该在 billing
之前。在内部 JSON 中,我也希望对其进行排序。例如在 billing
结构中 email
应该在 lastname
.
所以我正在寻找的答案是:
{
"address": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com",
"telephone": "+919999999999"
},
"billing": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com"
},
"deliveryMode": "API",
"failureUrl": "https://baseurl/ordercomplete/failure",
"payments": [
{
"code": "abcd",
"amount": 500
}
],
"products": [
{
"sku": "sampleSKU",
"price": 500,
"qty": 1,
"currency": 356,
"giftMessage": "",
"theme": ""
}
],
"refno": "abcd123",
"successUrl": "https://baseurl/ordercomplete/success",
"syncOnly": true
}
我想我可以通过创建多个具有所有字段的 POJO class 然后实现一个按字母顺序排序的比较器来做到这一点。但是这种做法即使请求体的参数中的单个字段发生变化也会变得非常困难。
所以我在想我们能不能用一些更好的方法来做到这一点,而不必担心字段结构。
您可以使用 Jackson ObjectMapper 并将 ObjectMapper 配置为
om.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
希望有用。