Dan*_*ker 310 ruby json ruby-on-rails
我正在寻找一种简单的方法来解析JSON,提取一个值并将其写入Rails中的数据库.
特别是我正在寻找的是一种shortUrl从bit.ly API返回的JSON中提取的方法:
{
"errorCode": 0,
"errorMessage": "",
"results":
{
"http://www.foo.com":
{
"hash": "e5TEd",
"shortKeywordUrl": "",
"shortUrl": "http://bit.ly/1a0p8G",
"userHash": "1a0p8G"
}
},
"statusCode": "OK"
}
Run Code Online (Sandbox Code Playgroud)
然后使用shortUrl并将其写入与long URL关联的ActiveRecord对象.
这是我完全可以在概念中思考的事情之一,当我坐下来执行时,我意识到我有很多需要学习的东西.
pgu*_*rio 445
这些答案有点过时了.所以我给你:
hash = JSON.parse string
Run Code Online (Sandbox Code Playgroud)
Rails的应该会自动加载json模块你,所以你并不需要添加require 'json'.
Mil*_*ota 187
在Rails中解析JSON非常简单:
parsed_json = ActiveSupport::JSON.decode(your_json_string)
Run Code Online (Sandbox Code Playgroud)
假设,您想要与shortUrl关联的对象是Site对象,它有两个属性 - short_url和long_url.要获得shortUrl并将其与相应的Site对象相关联,您可以执行以下操作:
parsed_json["results"].each do |longUrl, convertedUrl|
site = Site.find_by_long_url(longUrl)
site.short_url = convertedUrl["shortUrl"]
site.save
end
Run Code Online (Sandbox Code Playgroud)
lil*_*llq 55
这个答案很老了.pguardiario得到了它.
要检查的一个站点是Ruby的JSON实现.这个站点提供了一个可以为更快的C扩展变体安装的gem.
根据他们的文档页面的基准,他们声称它比它快21.500倍ActiveSupport::JSON.decode
代码与Milan Novota对这个gem的答案相同,但解析只是:
parsed_json = JSON(your_json_string)
Run Code Online (Sandbox Code Playgroud)
Jam*_*Lim 19
这是2013年的更新.
Ruby 1.9有一个带C扩展的默认JSON gem.你可以用它
require 'json'
JSON.parse ''{ "x": "y" }'
# => {"x"=>"y"}
Run Code Online (Sandbox Code Playgroud)
该parse!变体可用于安全来源.还有其他宝石,可能比默认实现更快.请参阅multi_json以获取列表.
Rails的现代版本使用multi_json,这是一个自动使用最快的JSON gem的gem.因此,推荐的方法是使用
object = ActiveSupport::JSON.decode json_string
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅ActiveSupport :: JSON.特别是,方法源中的重要一行是
data = MultiJson.load(json, options)
Run Code Online (Sandbox Code Playgroud)
然后在您的Gemfile中,包含您要使用的gem.例如,
group :production do
gem 'oj'
end
Run Code Online (Sandbox Code Playgroud)
小智 9
这可以按如下方式完成,只需要使用JSON.parse,然后您就可以使用索引正常遍历它。
#ideally not really needed, but in case if JSON.parse is not identifiable in your module
require 'json'
#Assuming data from bitly api is stored in json_data here
json_data = '{
"errorCode": 0,
"errorMessage": "",
"results":
{
"http://www.foo.com":
{
"hash": "e5TEd",
"shortKeywordUrl": "",
"shortUrl": "http://whateverurl",
"userHash": "1a0p8G"
}
},
"statusCode": "OK"
}'
final_data = JSON.parse(json_data)
puts final_data["results"]["http://www.foo.com"]["shortUrl"]
Run Code Online (Sandbox Code Playgroud)
Ruby捆绑的JSON本身就能展现出一点魔力.
如果您有一个包含要解析的JSON序列化数据的字符串:
JSON[string_to_parse]
Run Code Online (Sandbox Code Playgroud)
JSON将查看该参数,看它是一个String并尝试解码它.
同样,如果您想要序列化的哈希或数组,请使用:
JSON[array_of_values]
Run Code Online (Sandbox Code Playgroud)
要么:
JSON[hash_of_values]
Run Code Online (Sandbox Code Playgroud)
JSON将序列化它.to_json如果要避免方法的视觉相似性,也可以使用该[]方法.
这里有些例子:
hash_of_values = {'foo' => 1, 'bar' => 2}
array_of_values = [hash_of_values]
JSON[hash_of_values]
# => "{\"foo\":1,\"bar\":2}"
JSON[array_of_values]
# => "[{\"foo\":1,\"bar\":2}]"
string_to_parse = array_of_values.to_json
JSON[string_to_parse]
# => [{"foo"=>1, "bar"=>2}]
Run Code Online (Sandbox Code Playgroud)
如果您使用JSON,您可能会注意到它是YAML的一个子集,实际上YAML解析器正在处理JSON.你也可以这样做:
require 'yaml'
YAML.load(string_to_parse)
# => [{"foo"=>1, "bar"=>2}]
Run Code Online (Sandbox Code Playgroud)
如果您的应用程序正在解析YAML和JSON,您可以让YAML处理两种类型的序列化数据.
| 归档时间: |
|
| 查看次数: |
275168 次 |
| 最近记录: |