[英]Consume JSON - Shopify API response
I'm trying to work with what I think is a JSON response from Shopify. 我正在尝试使用Shopify的JSON响应。 It looks like this:
看起来像这样:
=> #<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>
The response I get from searching for a product via the Shopify API is of the class: 通过Shopify API搜索产品得到的响应属于此类:
ActiveResource::Collection
I first tried turning that to JSON with .to_json but thats just a string and I can't loop thru it easily. 我首先尝试使用.to_json将其转换为JSON,但这只是一个字符串,我无法轻松遍历它。
I then tried to turn it to an array with .to_a but now I can't get into the data.. 然后,我尝试使用.to_a将其转换为数组,但现在我无法进入数据。
The original response is in the x variable and now its an array.. If I try 原始响应位于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>
The data you show is an array of Product instances. 您显示的数据是产品实例的数组。 Simply take the data, which I'll call
data
, and loop through it to get each product. 只需获取数据(我将其称为
data
,然后遍历data
以获取每种产品。
data.each do |product|
puts product.title
puts product.vendor
end
You can obviously extend your functionality from here. 您显然可以从这里扩展功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.