Yelp JSON 输出未转换为 ruby​​ 哈希

发布于 2024-12-11 16:14:02 字数 2339 浏览 0 评论 0原文

这是我使用 Signet 的 YELP 客户端,但是一旦收到响应,我就无法转换为 ruby​​ 哈希来检查响应元素。

require 'rubygems'
require 'json'
require 'net/http'


client = Signet::OAuth1::Client.new(
  :client_credential_key =>
    'xxxxxxxxxxxxxxxxxxx',
  :client_credential_secret =>
    'xxxxxxxxxxxxxxxxxxx',
  :token_credential_key =>
    'xxxxxxxxxxxxxxxxxxx',
  :token_credential_secret => 
    'xxxxxxxxxxxxxxxxxxx'
)
response = client.fetch_protected_resource(
  :uri => 'http://api.yelp.com/v2/search?term=food&location=san+francisco'
)
# The Rack response format is used here
status, headers, body = response
puts body["businesses"]

错误: `[]':无法将字符串转换为整数(类型错误)

主体以漂亮的 JSON 格式打印良好,但我可以做 body["businesses"] 例如 JSON.parse(body).inspect 也不起作用。

顺便说一句,主体输出本身显示为 JSON 格式,但 JSON.parse(body) 不产生哈希

puts body

{"region":{"span":{"latitude_delta":0.0,"longitude_delta":0.0},"center":{"latitude":37.660418999999997,"longitude":-121.876508}},"total":853,"businesses":[{"rating":4.0,"mobile_url":"http://m.yelp.com/biz/TT1t4oHeZmqkoiuwgCN4bQ","rating_img_url":"http://media2.ak.yelpcdn.com/static/201012164084228337/img/ico/stars/stars_4.png","review_count":150,"name":"India Garden","rating_img_url_small":"http://media2.ak.yelpcdn.com/static/20101216418129184/img/ico/stars/stars_small_4.png","url":"http://www.yelp.com/biz/india-garden-pleasanton-2","phone":"9254854800","snippet_text":"We went to this place without seeing any reviews while we returning to San Jose from Cache Creek in Brooks. This place looks like a house which was...","image_url":"http://s3-media4.ak.yelpcdn.com/bphoto/8iFj1S9YaU5IdUazwZOG8A/ms.jpg","snippet_image_url":"http://s3-media3.ak.yelpcdn.com/photo/d2TovvsTn2eUw4xqTB4jyw/ms.jpg","display_phone":"+1-925-485-4800","rating_img_url_large":"http://media4.ak.yelpcdn.com/static/20101216169592178/img/ico/stars/stars_large_4.png","id":"india-garden-pleasanton-2","categories":[["Indian","indpak"],["Pakistani","pakistani"]],"location":{"cross_streets":"Main St & Neal St","city":"Pleasanton","display_address":["210 Rose Ave","(b/t Main St & Neal St)","Pleasanton, CA 94566"],"geo_accuracy":8,"postal_code":"94566","country_code":"US","address":["210 Rose Ave"],"coordinate":{"latitude":37.660418999999997,"longitude":-121.876508},"state_code":"CA"}}]}

Here is my YELP client using signet but once I get response, I'm not able to convert to ruby hash to inspect response element.

require 'rubygems'
require 'json'
require 'net/http'


client = Signet::OAuth1::Client.new(
  :client_credential_key =>
    'xxxxxxxxxxxxxxxxxxx',
  :client_credential_secret =>
    'xxxxxxxxxxxxxxxxxxx',
  :token_credential_key =>
    'xxxxxxxxxxxxxxxxxxx',
  :token_credential_secret => 
    'xxxxxxxxxxxxxxxxxxx'
)
response = client.fetch_protected_resource(
  :uri => 'http://api.yelp.com/v2/search?term=food&location=san+francisco'
)
# The Rack response format is used here
status, headers, body = response
puts body["businesses"]

Error:
`[]': can't convert String into Integer (TypeError)

Body prints fine in nice JSON format but I can do body["businesses"] for instance
JSON.parse(body).inspect is also not working.

Btw body outputs itself appears as JSON format but JSON.parse(body) doesn't produce hash

puts body

{"region":{"span":{"latitude_delta":0.0,"longitude_delta":0.0},"center":{"latitude":37.660418999999997,"longitude":-121.876508}},"total":853,"businesses":[{"rating":4.0,"mobile_url":"http://m.yelp.com/biz/TT1t4oHeZmqkoiuwgCN4bQ","rating_img_url":"http://media2.ak.yelpcdn.com/static/201012164084228337/img/ico/stars/stars_4.png","review_count":150,"name":"India Garden","rating_img_url_small":"http://media2.ak.yelpcdn.com/static/20101216418129184/img/ico/stars/stars_small_4.png","url":"http://www.yelp.com/biz/india-garden-pleasanton-2","phone":"9254854800","snippet_text":"We went to this place without seeing any reviews while we returning to San Jose from Cache Creek in Brooks. This place looks like a house which was...","image_url":"http://s3-media4.ak.yelpcdn.com/bphoto/8iFj1S9YaU5IdUazwZOG8A/ms.jpg","snippet_image_url":"http://s3-media3.ak.yelpcdn.com/photo/d2TovvsTn2eUw4xqTB4jyw/ms.jpg","display_phone":"+1-925-485-4800","rating_img_url_large":"http://media4.ak.yelpcdn.com/static/20101216169592178/img/ico/stars/stars_large_4.png","id":"india-garden-pleasanton-2","categories":[["Indian","indpak"],["Pakistani","pakistani"]],"location":{"cross_streets":"Main St & Neal St","city":"Pleasanton","display_address":["210 Rose Ave","(b/t Main St & Neal St)","Pleasanton, CA 94566"],"geo_accuracy":8,"postal_code":"94566","country_code":"US","address":["210 Rose Ave"],"coordinate":{"latitude":37.660418999999997,"longitude":-121.876508},"state_code":"CA"}}]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有你 2024-12-18 16:14:02

实际上我很确定,此时主体是一个数组,因为响应包含四个部分而不是三个部分,因此最后两个部分(一个数组)被放入主体对象中。

另外,Array 是我所知道的唯一核心对象,它抱怨 [] 参数不是整数。如果它是一个字符串,它将尝试正则表达式/包含匹配。

综上所述,body 是一个只有一个包含 String 的 Value 的数组。因此,要获取哈希值(来自 JSON),您必须 real_body = JSON.parse body[0]。然后你应该得到你的哈希值
real_body["businesses"] 放置您的企业(输出相当长,所以我不会在此处发布)

Actually i'm pretty sure, that body is an Array at this point, since response contains four parts and not three, so the last two parts (an array) are put into the body-object.

Also Array is the only core-object i know, which complains about an [] parameter being not an integer. If it was a string, it would try a regex/contain match.

So to sum up, body is an Array with only one Value containing a String. So to get your Hash (from JSON) you have to real_body = JSON.parse body[0]. Then you should get your hash and
real_body["businesses"] puts your businesses (the output is rather long so i will not be posting it here)

夏见 2024-12-18 16:14:02

此时 body 是一个字符串,而不是哈希值。 [] 运算符会抱怨,因为字符串 [] 运算符只接受一个整数,因此它试图将字符串转换为整数,但失败了。

编辑:您可以通过打印 body.class 来测试它

body is a string at this point, not a hash. The [] operator is complaining because the string [] operator only takes an integer, so it's trying to turn your string into an integer and failing.

edit: you can test this by just printing out body.class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文