Sort the JSON Body of HTTP Request alphabetically

huangapple go评论61阅读模式
英文:

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类,包含所有字段,然后实现一个比较器来按字母顺序进行排序。但是,这种做法会使得即使请求主体的单个字段发生更改,也会变得非常困难。

因此,我在思考是否有一种更好的方法,我们不必担心字段结构。

英文:

I am doing an API call with certain parameters. The body of the Request is something like this:

{
  "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"
}

I want to sort the parameters of the request alphabetically. The sorting should be done at outer level and inner level as well. For example, address should come before billing after the sort. Within the internal JSON also I want it to be sorted. For example in the billing struct email should come before lastname.

So the answer that I am looking for is:

{
	"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
}

I think I can do this by creating multiple POJO class having all the field and then implement a comparator which will sort it alphabetically. But this way of doing will make it very difficult even if a single field in the parameter of the request body change.

So I was thinking can we do it some better way where we do not have to worry about the field structure.

答案1

得分: 1

你可以使用Jackson ObjectMapper,并将ObjectMapper配置为

om.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

希望对您有帮助。

英文:

You could use Jackson ObjectMapper and configure ObjectMapper as

om.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

Hope it was useful.

huangapple
  • 本文由 发表于 2020年8月7日 18:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定