【问题标题】:Java - Sort JSON output based on keyJava - 根据键对 JSON 输出进行排序
【发布时间】:2017-04-05 11:35:14
【问题描述】:

我有一些 JSON 响应给出以下内容。我需要做的是,我需要根据属性名称/键对 JSON 响应进行排序。示例 JSON 是,

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

上面的JSON需要根据属性进行排序。示例输出如下所示。

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

在 Java 中是否有任何库可以做到这一点?我们该怎么做呢?

【问题讨论】:

标签: java json


【解决方案1】:
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.

【讨论】:

  • 我正在考虑用 Java 编写一个解决方案。如果您将伪代码转换为 Java 代码,那就太好了。提前致谢。
猜你喜欢
  • 2018-04-23
  • 2015-10-27
  • 2021-08-09
  • 1970-01-01
  • 2013-03-27
  • 2011-04-05
  • 2011-12-11
  • 1970-01-01
  • 2014-04-27
相关资源
最近更新 更多