消费 JSON - Shopify API 响应

Consume JSON - Shopify API response

我正在尝试处理我认为来自 Shopify 的 JSON 回复。它看起来像这样:

=> #<ActiveResource::Collection:0x007f61f45c3840
 @elements=
  [#<ShopifyAPI::Product:0x007f61f45c3638
    @attributes=
     {"id"=>2156425793,
      "title"=>"Hue Women's Python Net Tight, Black, Small-Medium",
      "body_html"=>"Python pattern",
      "vendor"=>"HUE",
      "product_type"=>"Apparel",
      "created_at"=>"2015-10-02T09:42:07-04:00",
      "handle"=>"hue-womens-python-net-tight-black-small-medium",
      "updated_at"=>"2015-10-02T09:42:07-04:00",
      "published_at"=>"2015-10-02T09:42:07-04:00",
      "template_suffix"=>nil,
      "published_scope"=>"global",
      "tags"=>"",
  "variants"=>
   [#<ShopifyAPI::Variant:0x007f61f45c11a8
     @attributes=
      {"id"=>6989658561,
       "title"=>"First",
       "price"=>"18.00",
       "sku"=>"123",
       "position"=>1,
       "grams"=>0,
       "inventory_policy"=>"deny",
       "compare_at_price"=>"18.00",
       "fulfillment_service"=>"amazon_marketplace_web",
       "inventory_management"=>"shopify",
       "option1"=>"First",
       "option2"=>nil,
       "option3"=>nil,
       "created_at"=>"2015-10-02T09:42:07-04:00",
       "updated_at"=>"2015-10-02T09:42:07-04:00",
       "requires_shipping"=>true,
       "taxable"=>true,
       "barcode"=>nil,
       "inventory_quantity"=>1,
       "old_inventory_quantity"=>1,
       "image_id"=>nil,
       "weight"=>0.0,
       "weight_unit"=>"lb"},
     @persisted=true,
     @prefix_options={:product_id=>2156425793}>,
    #<ShopifyAPI::Variant:0x007f61f45b4d18
     @attributes=
      {"id"=>6989658625,
       "title"=>"Second",
       "price"=>"18.00",
       "sku"=>"345",
       "position"=>2,
       "grams"=>0,
       "inventory_policy"=>"deny",
       "compare_at_price"=>"18.00",
       "fulfillment_service"=>"amazon_marketplace_web",
       "inventory_management"=>"shopify",
       "option1"=>"Second",
       "option2"=>nil,
       "option3"=>nil,
       "created_at"=>"2015-10-02T09:42:07-04:00",
       "updated_at"=>"2015-10-02T09:42:07-04:00",
       "requires_shipping"=>true,
       "taxable"=>true,
       "barcode"=>nil,
       "inventory_quantity"=>3,
       "old_inventory_quantity"=>3,
       "image_id"=>nil,
       "weight"=>0.0,
       "weight_unit"=>"lb"},
     @persisted=true,
     @prefix_options={:product_id=>2156425793}>],
  "options"=>
   [#<ShopifyAPI::Option:0x007f61f45a92b0
     @attributes={"id"=>2550138369, "product_id"=>2156425793, 
     "name"=>"Title", "position"=>1, "values"=>["First", "Second"]},
     @persisted=true,
     @prefix_options={}>],
  "images"=>
   [#<ShopifyAPI::Image:0x007f61f459b390
     @attributes=
      {"id"=>5116928641,
       "position"=>1,
       "created_at"=>"2015-10-02T09:42:07-04:00",
       "updated_at"=>"2015-10-02T09:42:07-04:00",
       "src"=>"https://cdn.shopify.com/s/files/1/0842/7077/products
       /41ooqKhDYRL._UL1500.jpeg?v=1443793327",
       "variant_ids"=>[]},
     @persisted=true,
     @prefix_options={:product_id=>2156425793}>],
  "image"=>
   #<ShopifyAPI::Image:0x007f61f4598050
    @attributes=
     {"id"=>5116928641,
      "position"=>1,
      "created_at"=>"2015-10-02T09:42:07-04:00",
      "updated_at"=>"2015-10-02T09:42:07-04:00",
      "src"=>"https://cdn.shopify.com/s/files/1/0842/7077/products
       /41ooqKhDYRL._UL1500.jpeg?v=1443793327",
      "variant_ids"=>[]},
    @persisted=true,
    @prefix_options={:product_id=>2156425793}>},
@persisted=true,
@prefix_options={}>],
@original_params={:title=>"Hue Women's Python Net Tight"},
@resource_class=ShopifyAPI::Product>

我通过 Shopify API 搜索产品得到的响应是 class:

ActiveResource::Collection

我首先尝试使用 .to_json 将其转换为 JSON,但这只是一个字符串,我无法轻松地循环遍历它。

然后我尝试用 .to_a 将其转换为数组,但现在我无法进入数据..

原始响应在 x 变量中,现在它是一个数组。如果我尝试

x[0] - I get the original response back
x[1] - nil
X[0]["id] - NoMethodError: undefined method `[]' for 
            #<ShopifyAPI::Product:0x007f61f45c3638>
x["id"] - TypeError: no implicit conversion of String into Integer
x[0][0] - NoMethodError: undefined method `[]' for 
          #<ShopifyAPI::Product:0x007f61f45c3638>
x[0].class - Shopify::Product which is a ActiveResource::Collection
x[0].to_array - NoMethodError: undefined method `to_array' for 
                #<ShopifyAPI::Product:0x007f61f45c3638>

您显示的数据是一组 Product 实例。只需获取数据,我将其称为 data,并循环遍历它以获取每个产品。

data.each do |product|

  puts product.title
  puts product.vendor

end

您显然可以从这里扩展您的功能。