【问题标题】:how to retrieve JSON object from the JSON array in ruby如何从 ruby​​ 中的 JSON 数组中检索 JSON 对象
【发布时间】:2016-11-13 04:31:43
【问题描述】:

我收到以下 JSON 响应。在这个 JSON 中,我将“结果”作为 JSON 数组。我想从这个数组中检索并显示每个实例名称和相应状态的值。我该怎么做?

{
  "oradbInstance" : {
    "oraDBHost" : "",
    "oraDBPort" : "",
    "oraDBSid" : "",
    "oraDBPass" : "",
    "oraDBJdePass" : "",
    "oraDBStatus" : "",
    "oraDBDepComponent" : "",
    "oraSHARED" : false,
    "oraADF" : false,
    "oraOVR" : false,
    "oradbSchema" : {
      "oraPROD" : false,
      "oraPRIST" : false,
      "oraCRP" : false,
      "oraDEV" : false
    },
    "oradbDemoSchema" : {
      "oraPRODDEMO" : false,
      "oraPRISTDEMO" : false,
      "oraCRPDEMO" : false,
      "oraDEVDEMO" : false
    }
  },
  "result" : [ {
    "instanceName" : "ent6327",
    "targetType" : "entserver",
    "status" : "STOPPED"
  }, {
    "instanceName" : "ent790",
    "targetType" : "entserver",
    "status" : "STOPPED"
  }, {
    "instanceName" : "920_ENT_6017",
    "targetType" : "entserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "ent7943",
    "targetType" : "entserver",
    "status" : "STOPPED"
  }, {
    "instanceName" : "920_JAS_8082",
    "targetType" : "webserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "ENT6547",
    "targetType" : "entserver",
    "status" : "STOPPED"
  }, {
    "instanceName" : "ent4563",
    "targetType" : "entserver",
    "status" : "STOPPED"
  }, {
    "instanceName" : "ent6021",
    "targetType" : "entserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "AIS_0005",
    "targetType" : "restserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "DEN00KNL_DEP_920",
    "targetType" : "depserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "wls1213",
    "targetType" : "owl_1212",
    "status" : "RUNNING"
  }, {
    "instanceName" : "HTML_8792",
    "targetType" : "webserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "home",
    "targetType" : "mgmtconsole",
    "status" : "RUNNING"
  }, {
    "instanceName" : "ent6060_Win",
    "targetType" : "entserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "RTE_0004",
    "targetType" : "rteserver",
    "status" : "RUNNING"
  }, {
    "instanceName" : "ent6363",
    "targetType" : "entserver",
    "status" : "STOPPED"
  } ]
}

任何有用的建议

【问题讨论】:

  • 欢迎来到 Stack Overflow。我们希望看到您尝试解决此问题,而不是为您编写代码。请阅读“How to Ask”(包括链接页面)和“minimal reproducible example”。

标签: ruby json


【解决方案1】:
require 'json'
JSON.parse(input)['result'].map do |h|
  [h['instanceName'], h['status']]
end.to_h
#⇒ {
#      "920_ENT_6017" => "RUNNING",
#      "920_JAS_8082" => "RUNNING",
#          "AIS_0005" => "RUNNING",
#  "DEN00KNL_DEP_920" => "RUNNING",
#           "ENT6547" => "STOPPED",
#         "HTML_8792" => "RUNNING",
#          "RTE_0004" => "RUNNING",
#           "ent4563" => "STOPPED",
#           "ent6021" => "RUNNING",
#       "ent6060_Win" => "RUNNING",
#           "ent6327" => "STOPPED",
#           "ent6363" => "STOPPED",
#            "ent790" => "STOPPED",
#           "ent7943" => "STOPPED",
#              "home" => "RUNNING",
#           "wls1213" => "RUNNING"
# }

【讨论】:

  • 我可以用表格格式打印带有标题实例名称和状态的输出
  • 是的,你绝对可以。
猜你喜欢
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
相关资源
最近更新 更多