我试图使用我认为来自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搜索产品所获得的响应属于以下类:
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>
答案 0 :(得分:1)
您显示的数据是一组Product实例。只需获取我称之为data
的数据,然后遍历它以获取每个产品。
data.each do |product|
puts product.title
puts product.vendor
end
您显然可以从此处扩展您的功能。