简体   繁体   中英

Java - Sort JSON output based on key

I have some JSON response giving down the below. What I need to do is, I need to sort the JSON response based on the property name/ key. The example JSON is,

{
  "cometo-yellow:profiles": {
    "upstream-speed-rate-profile": [
      {
        "name": "default-up-speed-rate-profile",
        "maximum-net-speed-rate": 200000,
        "maximum-gamma-speed-rate": 200000,
        "minimum-expected-throughput": 2000,
        "minimum-gamma-speed-rate": 2000
      },
      {
        "name": "test",
        "maximum-gamma-speed-rate": 8888,
        "minimum-gamma-speed-rate": 2222
      }
    ],
    "downstream-speed-rate-profile": [
      {
        "name": "default-down-speed-rate-profile",
        "maximum-net-speed-rate": 800000,
        "maximum-gamma-speed-rate": 800000,
        "minimum-expected-throughput": 2000,
        "minimum-gamma-speed-rate": 2000
      }
    ],
    "yellow-rate-adaptation-profile": [
      {
        "name": "default-yellow-rate-adaptation-profile",
        "downstream": {
          "minimum-degraded-tones": 1,
          "time-window": 8,
          "vendor-triggering-criteria-enabled": false,
          "uncorrectable-dtu": 150
        },
        "upstream": {
          "minimum-degraded-tones": 1,
          "time-window": 8,
          "vendor-triggering-criteria-enabled": false,
          "uncorrectable-dtu": 150
        }
      }
    ]
  }
}

The above JSON needs to be sorted based on the property. The sample output looks below.

{
  "cometo-yellow:profiles": {
    "downstream-speed-rate-profile": [
        "maximum-gamma-speed-rate": 800000,
        "maximum-net-speed-rate": 800000
        "minimum-expected-throughput": 2000,
        "minimum-gamma-speed-rate": 2000, 
        "name": "default-down-speed-rate-profile"
    ],
    "upstream-speed-rate-profile": [
        <<SORTED>>
    ],
    "yellow-rate-adaptation-profile": [
        <<SORTED>>
    ]
   }
}

Is there any library for doing this in Java ? How can we do this ?

var SortvalueArray = JSON.split() to separate the string only and then 
use the SortvalueArray variable with index to do operations on it.
We can also use the var sortvalues= JSON.parse(SortvalueArray[<index>]) method to get key/values.
Then use the sortvalues to get the value of cookie object using .<cookieobject> to the sortvalues then use this key value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM