lundi 16 novembre 2015

How to loop through JSON in ruby

Below details is my shipment details. I want to loop through this JSON and get each checkpoint details.

How can I do this with Ruby

This is my controller:

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

this is my view page where i get json data

= @asd.to_json`

This is the 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
        }

    }
  }
}

Aucun commentaire:

Enregistrer un commentaire