Ruby-on-rails 使用 HTTParty 在控制器中解析 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18320642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing JSON in Controller w/ HTTParty
提问by gbdev
In my controller I have the following code...
在我的控制器中,我有以下代码...
response = HTTParty.get('https://graph.facebook.com/zuck')
logger.debug(response.body.id)
I am getting a NoMethodError / undefined method `id'
我收到一个 NoMethodError / 未定义的方法 `id'
If I do...
如果我做...
logger.debug(response.body)
It outputs as it should...
它输出它应该......
{"id":"4","name":"Mark Zuckerberg","first_name":"Mark","last_name":"Zuckerberg","link":"http:\/\/www.facebook.com\/zuck","username":"zuck","gender":"male","locale":"en_US"}
One would think it's response.body.id, but obviously that's not working. Thanks in advance!
有人会认为它是response.body.id,但显然这是行不通的。提前致谢!
回答by Niall Paterson
Try this:
尝试这个:
body = JSON.parse(response.body)
id = body["id"]
For this kind of thing, I'd recommend either a) using Koalaor b) create a class using httparty. You can then set format: jsonto auto parse the returned json. See hereand here
对于这种事情,我建议 a) 使用Koala或 b) 使用 httparty 创建一个类。然后您可以设置format: json为自动解析返回的 json。看到这里和这里
回答by bonh
You can force the response to be treated as JSON using HTTParty.getlike so:
您可以使用HTTParty.get如下方式强制将响应视为 JSON :
response = HTTParty.get("http://itunes.apple.com/search",
{query: {term: 'tame impala'}, format: :json})
response['results'][0]['trackName']
=> "Let It Happen"
回答by Pablo Cantero
You can use response['id']in case that the response Content-Typeis application/jsonor also response.parse_responseto get a Hash generated from the JSON payload.
您可以response['id']在响应Content-Type是application/json或也response.parse_response用于获取从 JSON 有效负载生成的哈希的情况下使用。
response = HTTParty.get('https://graph.facebook.com/zuck')
payload = response.parsed_response
logger.debug(payload['id'])
Note: parsed_responseis a Hash, only if the response Content-Typeis application/json, otherwise HTTPartywill return it as a string. For enforcing a Hash, in case the response does not return application/json, you can pass the formatas a parameter HTTParty.get(url, format: :json).
注意:parsed_response是一个哈希,只有当响应Content-Type是application/json,否则HTTParty将它作为一个字符串返回。为了强制执行哈希,如果响应没有返回application/json,您可以将 传递format为参数HTTParty.get(url, format: :json)。
回答by Rimian
HTTParty should automatically parse the content based on the content type returned. Something fishy seems to be going on with zuck's json.
HTTParty 应该根据返回的内容类型自动解析内容。zuck 的 json 似乎发生了一些可疑的事情。
pry(main)> HTTParty.get('https://graph.facebook.com/zuck')
=> "{\"id\":\"4\",\"first_name\":\"Mark\",\"gender\":\"male\",\"last_name\":\"Zuckerberg\",\"link\":\"https:\/\/www.facebook.com\/zuck\",\"locale\":\"en_US\",\"name\":\"Mark Zuckerberg\",\"username\":\"zuck\"}"
But this works OK:
但这可以正常工作:
pry(main)> HTTParty.get('http://echo.jsontest.com/foo/bar/baz/foo')
=> {"baz"=>"foo", "foo"=>"bar"}
Don't forget to require 'httparty'if you're trying that in the console yourself.
require 'httparty'如果您自己在控制台中尝试,请不要忘记。

