用ruby解析API输出

时间:2018-10-03 02:21:07

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

道歉,如果这是非常基本的,对红宝石来说是全新的。

以下是我使用curl时得到的样本响应,需要从下面的输出中获取bodycreated_at的值。

当我尝试检查值的类型时,puts返回true表示(字符串),false表示哈希和数组

`#puts value.is_a?(Hash)
#puts value.is_a?(Array)
#puts value.is_a?(String)`

不确定如何从下面的输出中获取价值,请在此处帮助完成第一步/想法,将尝试并再次尝试进一步解决问题

样品呼叫      curl https://api.statuspage.io/v1/pages/qfn30z5r6s5h/incidents.json \ -H "Authorization: OAuth 2a7b9d4aac30956d537ac76850f4d78de30994703680056cc103862d53cf8074"

SAMPLE RESPONSE
  [
    {
      "created_at": "2013-04-21T11:45:33-06:00",
      "id": "tks5n8x7w24h",
      "impact": "none",
      "impact_override": null,
      "incident_updates": [
        {
          "body": "We will be performing a data layer migration from our existing Postgres system over to our new, multi-region, distributed Riak cluster. The application will be taken offline during the entirety of this migration. We apologize in advance for the inconvenience",
          "created_at": "2013-04-21T11:45:33-06:00",
          "display_at": "2013-04-21T11:45:33-06:00",
          "id": "kb4fpktpqm0l",
          "incident_id": "tks5n8x7w24h",
          "status": "scheduled",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:45:33-06:00",
          "wants_twitter_update": false,
          "affected_components": [
            {
              "code": "ftgks51sfs2d",
              "name": "API",
              "old_status": "operational",
              "new_status": "operational"
            }
          ]
        }
      ],
      "metadata": [
        "jira": {
          "issue_id": "value"
        }
      ],
      "monitoring_at": null,
      "name": "Data Layer Migration",
      "page_id": "jcm87b8scw0b",
      "postmortem_body": null,
      "postmortem_body_last_updated_at": null,
      "postmortem_ignored": true,
      "postmortem_notified_subscribers": false,
      "postmortem_notified_twitter": false,
      "postmortem_published_at": null,
      "resolved_at": null,
      "scheduled_auto_in_progress": false,
      "scheduled_auto_completed": false,
      "scheduled_for": "2013-05-04T01:00:00-06:00",
      "scheduled_remind_prior": false,
      "scheduled_reminded_at": null,
      "scheduled_until": "2013-05-04T03:00:00-06:00",
      "shortlink": "",
      "status": "scheduled",
      "updated_at": "2013-04-21T11:45:33-06:00"
    },
    {
      "created_at": "2013-04-21T11:04:28-06:00",
      "id": "cz46ym8qbvwv",
      "impact": "critical",
      "impact_override": null,
      "incident_updates": [
        {
          "body": "A postmortem analysis has been posted for this incident.",
          "created_at": "2013-04-21T11:42:31-06:00",
          "display_at": "2013-04-21T11:42:31-06:00",
          "id": "dn051mnj579k",
          "incident_id": "cz46ym8qbvwv",
          "status": "postmortem",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:42:31-06:00",
          "wants_twitter_update": false
        },
        {
          "body": "The application has returned to it's normal performance profile. We will be following up with a postmortem about future plans to guard against additional master database failure.",
          "created_at": "2013-04-21T11:16:38-06:00",
          "display_at": "2013-04-21T14:07:00-06:00",
          "id": "ppdqv1grhm64",
          "incident_id": "cz46ym8qbvwv",
          "status": "resolved",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:36:15-06:00",
          "wants_twitter_update": false,
          "affected_components": [
            {
              "code": "ftgks51sfs2d",
              "name": "API",
              "old_status": "degraded_performance",
              "new_status": "operational"
            }
          ]
        },
        {
          "body": "The slave database has been successfully promoted, but is running slow due to a cold query cache. The application is open and available for requests, but should will be performing in a degraded state for the next few hours. We will continue to monitor the situation.",
          "created_at": "2013-04-21T11:14:46-06:00",
          "display_at": "2013-04-21T11:14:46-06:00",
          "id": "j7ql87ktwnys",
          "incident_id": "cz46ym8qbvwv",
          "status": "monitoring",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:14:46-06:00",
          "wants_twitter_update": false,
          "affected_components": [
            {
              "code": "ftgks51sfs2d",
              "name": "API",
              "old_status": "major_outage",
              "new_status": "degraded_performance"
            }
          ]
        },
        {
          "body": "The slave database is 60% through it's recovery process. We will provide another update once the application is back up.",
          "created_at": "2013-04-21T11:08:42-06:00",
          "display_at": "2013-04-21T11:08:42-06:00",
          "id": "xzgd3y9zdzt9",
          "incident_id": "cz46ym8qbvwv",
          "status": "identified",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:08:42-06:00",
          "wants_twitter_update": false,
          "affected_components": [
            {
              "code": "ftgks51sfs2d",
              "name": "API",
              "old_status": "major_outage",
              "new_status": "major_outage"
            }
          ]
        },
        {
          "body": "The master database server could not boot due to a corrupted EBS volume. We are in the process of failing over to the slave database. ETA for the application recovering is 5 minutes.",
          "created_at": "2013-04-21T11:06:27-06:00",
          "display_at": "2013-04-21T11:06:27-06:00",
          "id": "9307nsfg3dxd",
          "incident_id": "cz46ym8qbvwv",
          "status": "identified",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:06:27-06:00",
          "wants_twitter_update": false,
          "affected_components": [
            {
              "code": "ftgks51sfs2d",
              "name": "API",
              "old_status": "major_outage",
              "new_status": "major_outage"
            }
          ]
        },
        {
          "body": "We're investigating an outage with our master database server.",
          "created_at": "2013-04-21T11:04:28-06:00",
          "display_at": "2013-04-21T11:04:28-06:00",
          "id": "dz959yz2nd4l",
          "incident_id": "cz46ym8qbvwv",
          "status": "investigating",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:04:29-06:00",
          "wants_twitter_update": false,
          "affected_components": [
            {
              "code": "ftgks51sfs2d",
              "name": "API",
              "old_status": "operational",
              "new_status": "major_outage"
            }
          ]
        }
      ],
      "metadata": [
        "jira": {
          "issue_id": "value"
        }
      ],
      "monitoring_at": "2013-04-21T11:14:46-06:00",
      "name": "Master Database Failure",
      "page_id": "jcm87b8scw0b",
      "postmortem_body": "##### Issue\r\n\r\nAt approximately 17:02 UTC on 2013-04-21, our master database server unexpectedly went unresponsive to all network traffic. A reboot of the machine at 17:05 UTC resulted in a failed mount of a corrupted EBS volume, and we made the decision at that time to fail over the slave database.\r\n\r\n##### Resolution\r\n\r\nAt 17:12 UTC, the slave database had been successfully promoted to master and the application recovered enough to accept web traffic again. A new slave database node was created and placed into the rotation to guard against future master failures. The promoted slave database performed slowly for the next couple of hours as the query cache began to warm up, and eventually settled into a reasonable performance profile around 20:00 UTC.\r\n\r\n##### Future Mitigation Plans\r\n\r\nOver the past few months, we've been working on an overhaul to our data storage layer with a migration from a Postgres setup to a distributed, fault-tolerant, multi-region data layer using Riak. This initiative has been prioritized, and the migration will be performed in the coming weeks. We will notify our clients of the scheduled downtime via an incident on this status site, and via a blog post.",
      "postmortem_body_last_updated_at": "2013-04-21T17:41:00Z",
      "postmortem_ignored": false,
      "postmortem_notified_subscribers": false,
      "postmortem_notified_twitter": false,
      "postmortem_published_at": "2013-04-21T17:42:31Z",
      "resolved_at": "2013-04-21T14:07:00-06:00",
      "scheduled_auto_in_progress": false,
      "scheduled_auto_completed": false,
      "scheduled_for": null,
      "scheduled_remind_prior": false,
      "scheduled_reminded_at": null,
      "scheduled_until": null,
      "shortlink": "",
      "status": "postmortem",
      "updated_at": "2013-04-21T11:42:31-06:00"
    },
    {
      "created_at": "2013-04-01T12:00:00-06:00",
      "id": "2ggpd60zvx3c",
      "impact": "none",
      "impact_override": null,
      "incident_updates": [
        {
          "body": "At approximately 6:55 PM, our network provider at ServerCo experienced a brief network outage at their New Jersey data center. The network outage lasted approximately 14 minutes, and all web requests during that time were not received. No data was lost, and the system recovered once the network outage at ServerCo was repaired.",
          "created_at": "2013-04-21T11:02:00-06:00",
          "display_at": "2013-04-21T11:02:00-06:00",
          "id": "mkfzp9swbk4z",
          "incident_id": "2ggpd60zvx3c",
          "status": "investigating",
          "twitter_updated_at": null,
          "updated_at": "2013-04-21T11:02:00-06:00",
          "wants_twitter_update": false
        }
      ],
      "metadata": [
        "jira": {
          "issue_id": "value"
        }
      ],
      "monitoring_at": null,
      "name": "Brief Network Outage",
      "page_id": "jcm87b8scw0b",
      "postmortem_body": null,
      "postmortem_body_last_updated_at": null,
      "postmortem_ignored": false,
      "postmortem_notified_subscribers": false,
      "postmortem_notified_twitter": false,
      "postmortem_published_at": null,
      "resolved_at": null,
      "scheduled_auto_in_progress": false,
      "scheduled_auto_completed": false,
      "scheduled_for": null,
      "scheduled_remind_prior": false,
      "scheduled_reminded_at": null,
      "scheduled_until": null,
      "shortlink": "",
      "status": "resolved",
      "updated_at": "2013-04-01T12:00:00-06:00"
    }
  ]

1 个答案:

答案 0 :(得分:1)

它是JSON。由于您使用的是Rails,因此只需调用

JSON.parse(value)

这将返回多个散列数组,您将可以进一步映射。