如何在ruby

时间:2015-11-10 11:52:57

标签: json ruby-on-rails-3

以下详细信息是我的货件详情。我想遍历这个JSON并获取每个检查点的详细信息。

如何使用Ruby

执行此操作

这是我的控制者:

@asd = AfterShip::V4::Tracking.get('ups', '1Z31Y1Y90490064644')

这是我查看json数据的视图页面

= @asd.to_json`

这是JSON:

{
  "meta": {
    "code": 200
  },
  "data": {
    "tracking": {
      "id": "560b87b38c9079f272e98dfc",
      "created_at": "2015-09-30T06:56:51+00:00",
      "updated_at": "2015-09-30T06:56:55+00:00",
      "last_updated_at": "2015-09-30T06:56:55+00:00",
      "tracking_number": "1Z31Y1Y90490064644",
      "slug": "ups",
      "active": false,
      "android": [

      ],
      "custom_fields": null,
      "customer_name": null,
      "delivery_time": 7,
      "destination_country_iso3": "GBR",
      "emails": [
        "pu@l.com"
      ],
      "expected_delivery": null,
      "ios": [

      ],
      "note": null,
      "order_id": null,
      "order_id_path": null,
      "origin_country_iso3": "HKG",
      "shipment_package_count": 1,
      "shipment_pickup_date": "2015-08-24T16:00:00",
      "shipment_delivery_date": "2015-09-01T14:22:00",
      "shipment_type": "UPS SAVER",
      "shipment_weight": 0.5,
      "shipment_weight_unit": "kg",
      "signed_by": "MANTON (RESIDENTIAL)",
      "smses": [

      ],
      "source": "api",
      "tag": "Delivered",
      "title": "1Z31Y1Y90490064644",
      "tracked_count": 1,
      "unique_token": "bkGcaTOJDe",
      "checkpoints": [
        {
          "slug": "ups",
          "city": null,
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "HK",
          "country_name": "HK",
          "message": "BILLING INFORMATION RECEIVED",
          "country_iso3": "HKG",
          "tag": "InfoReceived",
          "checkpoint_time": "2015-08-25T23:05:47",
          "coordinates": [

          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": null,
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": null,
          "country_name": null,
          "message": "YOUR PACKAGE WAS RELEASED BY THE CLEARING AGENCY.",
          "country_iso3": null,
          "tag": "InTransit",
          "checkpoint_time": "2015-08-27T16:57:00",
          "coordinates": [

          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": null,
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": null,
          "country_name": null,
          "message": "YOUR PACKAGE WAS RELEASED BY THE CLEARING AGENCY.",
          "country_iso3": null,
          "tag": "InTransit",
          "checkpoint_time": "2015-08-27T17:07:00",
          "coordinates": [

          ],
          "state": null,
          "zip": null
        }

    }
  }
}

1 个答案:

答案 0 :(得分:0)

您在这里得到的是Hash类的实例(已经解析了响应JSON)。

tracking = AfterShip::V4::Tracking.get('ups', '1Z31Y1Y90490064644')
tracking['data']['tracking']['id']
# => "560b87b38c9079f272e98dfc"