【发布时间】:2013-12-31 17:46:43
【问题描述】:
有人成功使用过这个API吗?尝试构建价格比较功能并且无法将 json 数据解析为 rails 数据结构。我正在尝试将结果过滤为仅卖家和最新报价。有人有解决办法吗?
我的控制器
sem3.products_field("search", "green toys tractor")
sem3.products_field( "sitedetails", "name")
# Make the query
@productsHash = sem3.get_products()
在视图中
<%= @productsHash %>
我能够看到整个 json 哈希,我尝试使用 json gem 对其进行解析。
JSON.parse(@productsHash)
但想知道是否有人可以分享一些他们自己的示例代码。
我的结果
{"total_results_count"=>9189, "code"=>"OK", "offset"=>0, "results_count"=>10, "results"=>[{"cat_id"=>"20823", "ean"=>"0649241988953", "model"=>"UMB13 A1342 GREEN", "offers_total"=>19, "weight"=>"340194.28", "category"=>"笔记本电脑和上网本电脑配件", " name"=>"Green mCover® Hard Shell Cover Case + Free Keyboard Skin for A1342 White Unibody MacBook 13", "updated_at"=>1387963145, "width"=>"335.28", "upc"=>"649241988953" , "brand"=>"Green", "created_at"=>1347717401, "color"=>"Green", "sem3_id"=>"1jAjbOWuXQmWG0wMwGg46m", "features"=>{"产品尺寸"=>"9.2 x 13.2 x 1.1 英寸;12 盎司"}, "height"=>"27.94", "length"=>"233.68", "manufacturer"=>"iPearl Inc, USA", "gtins"=>["00649241988953"] , "geo"=>["usa"], "price_currency"=>"USD", "price"=>"17.80", "sem3_help"=>"要查看此产品的图片链接和其他商家,请升级您的plan."}, {"cat_id"=>"20823", "ean"=>"0649241988403", "model"=>"ALU MB GREEN", "offers_total"=>25, "weight"=>"285763.19" , "分类ory"=>"笔记本电脑和上网本电脑配件", "name"=>"GREEN mCover Hard Shell Case for 13 英寸铝制 A1278 MacBook Pro (13.3 英寸对角屏幕)", "updated_at"=>1387817680, "price" =>“13.99”、“宽度”=>“28.45”、“upc”=>“649241988403”、“品牌”=>“绿色”、“created_at”=>1347554377、“sem3_id”=>“6VQYSLoouueo2ks0WUqIec2”、“特点"=>{"产品尺寸"=>"13 x 1.1 x 9.1 英寸; 10.1 盎司"}, "mpn"=>"ALU-MB-GREEN", "height"=>"231.65", "length"=>"330.20", "manufacturer"=>"iPearl Inc, USA", "gtins "=>["00649241988403"], "geo"=>["usa"], "price_currency"=>"USD", "sem3_help"=>"要查看此产品的图片链接和其他商家,请升级您的套餐."}, {"cat_id"=>"20823", "ean"=>"0872621001632", "model"=>"RT SPM14X02", "offers_total"=>509, "weight"=>"158757.33", " category"=>"Laptop & Netbook Computer Accessories", "name"=>"Green Onion Supply RT-SPM14X02 Ag2 Anti-glare Screen Protectoracs For Dell Alienware M14x", "updated_at"=>1388313241, "price"=>"28.87 ", "upc"=>"872621001632", "brand"=>"Green", "sem3_id"=>"2c8IsQrCnMq8KgEgiMo6yW", "created_at"=>1347808883, "features"=>{"包装内容"=>"MicroFiber布", "显示屏尺寸兼容性"=>"14\" 宽", "其他功能"=>"防眩光、防刮擦、可重复使用", "产品类型"=>"笔记本屏幕保护膜"}, " mpn"=>"RT-SPM14X02", "sitedetails"=>[{"sku"=>"17046069", "latestoffers"=>[{"seller"=>"Walma rt", "lastrecorded_at"=>1387704200, "firstrecorded_at"=>1379990800, "currency"=>"USD", "availability"=>"In stock", "price"=>"32.24", "id"=> "5pPydRLcRcRcUIIeu8iG4o4K"}, {"seller"=>"Walmart", "lastrecorded_at"=>1360662000, "firstrecorded_at"=>1360662000, "currency"=>"USD", "availability"=>"In stock", "id "=>"7Yf1biLaZkMcSwOyKUuS4m", "price"=>"31.24"}, {"seller"=>"Walmart", "lastrecorded_at"=>1358731800, "firstrecorded_at"=>1358731800, "currency"=>"USD", "availability"=>"In stock", "id"=>"51zIYD0tYeSECu4KGuKC2s", "price"=>"30.93"}], "name"=>"walmart.com", "offers_count"=>20, "url "=>"http://www.walmart.com/ip/17046069", "recentoffers_count"=>1}, {"offers_count"=>3, "sku"=>"1092214", "latestoffers"=>[{"seller"=>"NextWarehouse.com ", "lastrecorded_at"=>1384661400, "currency"=>"USD", "firstrecorded_at"=>1384661400, "availability"=>"Available", "id"=>"0QuY3kcCdagYaUeGwYyS2o", "price"=>"35.95 "}, {"seller"=>"NextWarehouse.com", "lastrecorded_at"=>1382359600, "currency"=>"USD", "firstrecorded_at"=>1382359600, "可用性"=>"有货", "价格"=>"36.13", "id"=>"2SLt8pdveGqGUUaAWW0w62"}, {"seller"=>"NextWarehouse.com", "lastrecorded_at"=>1379825800, "firstrecorded_at"=> 1379825800, "货币"=>"美元", "可用性"=>"可用", "id"=>"7eJC9XuXVuQ8GWSqWCIqKq", "价格"=>"35.95"}], "url"=>"http://www.nextwarehouse.com/item/?1092214_g10e" , "name"=>"nextwarehouse.com", "recentoffers_count"=>1}, {"offers_count"=>3, "sku"=>"1604730", "latestoffers"=>[{"seller"=>" Colamco", "lastrecorded_at"=>1384661400, "currency"=>"USD", "firstrecorded_at"=>1384661400, "availability"=>"Available", "id"=>"1ZNuX5bmpY4mACsIOqIg8K", "price"=>" 36.27"}, {"seller"=>"Colamco", "lastrecorded_at"=>1382359600, "currency"=>"USD", "firstrecorded_at"=>1382359600, "availability"=>"Available", "price"= >"36.60", "id"=>"39GVaekhIuOyAeyOyiKkCc"}, {"seller"=>"Colamco", "lastrecorded_at"=>1379825800, "firstrecorded_at"=>1379825800, "currency"=>"USD", "可用性"=>"有货", "id"=>"05CmUv60bxIQiumM0mWgUe", "price"=>"34.27"}]
【问题讨论】:
-
你遇到了什么麻烦?您的 JSON 解析出了什么问题?你放在那里的代码会崩溃吗?未显示预期结果?你在期待什么?
-
代码返回整个哈希。我尝试使用 json gem 来解析带有 JSON.parse(@productsHash) 的数据,但不知道如何提取我需要的数据。
-
你在问如何访问解析后的 JSON?
-
是的,想知道是否有人专门使用过这个 api
-
无论API如何访问JSON值都是一样的,只是嵌套和结构会改变。
标签: ruby-on-rails ruby json