mardi 10 novembre 2015

How to loop json array in ruby

Below details is my shipment details. So i want to loop through this json and get each checkpoint details .

How to do so and output through ruby.

This is my controller

class ShipmentsController < ApplicationController # require 'rubygems' # require 'aftership' def show @asd = AfterShip::V4::Tracking.get('ups', '1Z31Y1Y90490064644') end end this is my view page where i get json data

= @asd.to_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": [
        "prabhakar@langoor.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
        },
        {
          "slug": "ups",
          "city": "NORTH RODE",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "NORTH RODE, GB",
          "country_name": "NORTH RODE, GB",
          "message": "THE RECEIVER MUST PAY THE DUTIES OR TAXES DUE ON THE PACKAGE.",
          "country_iso3": "GBR",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-27T20:17:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "KWAI CHUNG",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "KWAI CHUNG, HK",
          "country_name": "KWAI CHUNG, HK",
          "message": "ORIGIN SCAN",
          "country_iso3": "HKG",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-27T21:38:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "KWAI CHUNG",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "KWAI CHUNG, HK",
          "country_name": "KWAI CHUNG, HK",
          "message": "DEPARTURE SCAN",
          "country_iso3": "HKG",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-28T00:02:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "CHEK LAP KOK",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "CHEK LAP KOK, HK",
          "country_name": "CHEK LAP KOK, HK",
          "message": "DEPARTURE SCAN",
          "country_iso3": "HKG",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-28T12:22:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "DUBAI",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "DUBAI, AE",
          "country_name": "DUBAI, AE",
          "message": "ARRIVAL SCAN",
          "country_iso3": "ARE",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-28T16:00:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "DUBAI",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "DUBAI, AE",
          "country_name": "DUBAI, AE",
          "message": "DEPARTURE SCAN",
          "country_iso3": "ARE",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-28T20:30:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "KOELN \u0026amp;\u0026#35;40;COLOGNE\u0026#41;",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "KOELN \u0026amp;\u0026#35;40;COLOGNE\u0026#41;, DE",
          "country_name": "KOELN \u0026amp;\u0026#35;40;COLOGNE\u0026#41;, DE",
          "message": "DEPARTURE SCAN",
          "country_iso3": "DEU",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-29T05:05:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "KOELN \u0026amp;\u0026#35;40;COLOGNE\u0026#41;",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "KOELN \u0026amp;\u0026#35;40;COLOGNE\u0026#41;, DE",
          "country_name": "KOELN \u0026amp;\u0026#35;40;COLOGNE\u0026#41;, DE",
          "message": "DEPARTURE SCAN",
          "country_iso3": "DEU",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-31T02:34:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "EAST MIDLANDS AIRPOR",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "EAST MIDLANDS AIRPOR, GB",
          "country_name": "EAST MIDLANDS AIRPOR, GB",
          "message": "ARRIVAL SCAN",
          "country_iso3": "GBR",
          "tag": "InTransit",
          "checkpoint_time": "2015-08-31T18:00:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "EAST MIDLANDS AIRPOR",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "EAST MIDLANDS AIRPOR, GB",
          "country_name": "EAST MIDLANDS AIRPOR, GB",
          "message": "DEPARTURE SCAN",
          "country_iso3": "GBR",
          "tag": "InTransit",
          "checkpoint_time": "2015-09-01T00:59:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "TAMWORTH",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "TAMWORTH, GB",
          "country_name": "TAMWORTH, GB",
          "message": "ARRIVAL SCAN",
          "country_iso3": "GBR",
          "tag": "InTransit",
          "checkpoint_time": "2015-09-01T01:41:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "TAMWORTH",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "TAMWORTH, GB",
          "country_name": "TAMWORTH, GB",
          "message": "DEPARTURE SCAN",
          "country_iso3": "GBR",
          "tag": "InTransit",
          "checkpoint_time": "2015-09-01T03:17:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "NORTH RODE",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "NORTH RODE, GB",
          "country_name": "NORTH RODE, GB",
          "message": "ARRIVAL SCAN",
          "country_iso3": "GBR",
          "tag": "InTransit",
          "checkpoint_time": "2015-09-01T04:51:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "NORTH RODE",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "NORTH RODE, GB",
          "country_name": "NORTH RODE, GB",
          "message": "OUT FOR DELIVERY",
          "country_iso3": "GBR",
          "tag": "OutForDelivery",
          "checkpoint_time": "2015-09-01T04:56:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "NORTH RODE",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "NORTH RODE, GB",
          "country_name": "NORTH RODE, GB",
          "message": "THE DRIVER WAS UNABLE TO COLLECT FUNDS ON THE FIRST DELIVERY ATTEMPT. A SECOND ATTEMPT WILL BE MADE.",
          "country_iso3": "GBR",
          "tag": "AttemptFail",
          "checkpoint_time": "2015-09-01T14:18:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": null
        },
        {
          "slug": "ups",
          "city": "HYDE",
          "created_at": "2015-09-30T06:56:55+00:00",
          "location": "HYDE, GB, SK14 5",
          "country_name": "HYDE, GB, SK14 5",
          "message": "DELIVERED",
          "country_iso3": "GBR",
          "tag": "Delivered",
          "checkpoint_time": "2015-09-01T14:22:00",
          "coordinates": [
            
          ],
          "state": null,
          "zip": "SK14 5"
        }
      ],
      "tracking_account_number": null,
      "tracking_destination_country": null,
      "tracking_key": null,
      "tracking_postal_code": null,
      "tracking_ship_date": null
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire