当前位置: 首页 > 工具软件 > JSON for Ruby > 使用案例 >

如何使用Ruby on Rails解析JSON? [重复]

焦兴为
2023-12-01

本文翻译自:How do I parse JSON with Ruby on Rails? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I'm looking for a simple way to parse JSON, extract a value and write it into a database in Rails. 我正在寻找一种简单的方法来解析JSON,提取一个值并将其写入Rails中的数据库。

Specifically what I'm looking for, is a way to extract shortUrl from the JSON returned from the bit.ly API: 特别是我正在寻找的是一种从bit.ly API返回的JSON中提取shortUrl的方法:

{
  "errorCode": 0,
  "errorMessage": "",
  "results":
  {
    "http://www.foo.com":
    {
       "hash": "e5TEd",
       "shortKeywordUrl": "",
       "shortUrl": "http://bit.ly/1a0p8G",
       "userHash": "1a0p8G"
    }
  },
  "statusCode": "OK"
}

And then take that shortUrl and write it into an ActiveRecord object associated with the long URL. 然后使用shortUrl并将其写入与long URL关联的ActiveRecord对象。

This is one of those things that I can think through entirely in concept and when I sit down to execute I realize I've got a lot to learn. 这是我完全可以在概念中思考的事情之一,当我坐下来执行时,我意识到我有很多需要学习的东西。


#1楼

参考:https://stackoom.com/question/7fDL/如何使用Ruby-on-Rails解析JSON-重复


#2楼

These answers are a bit dated. 这些答案有点过时了。 Therefore I give you: 所以我给你:

hash = JSON.parse string

Rails should automagically load the json module for you, so you don't need to add require 'json' . Rails的应该会自动加载json模块为你,这样你就不需要添加require 'json'


#3楼

You can try something like this: 你可以尝试这样的事情:

def details_to_json
{
  :id                    => self.id, 
  :credit_period_type    => self.credit_period_type,
  :credit_payment_period => self.credit_payment_period,

 }.to_json
end

#4楼

RUBY is case sensitive. RUBY区分大小写。

require 'json' # json must be lower case

JSON.parse(<json object>)  

for example 例如

JSON.parse(response.body) # JSON must be all upper-case

#5楼

Here is an update for 2013. 这是2013年的更新。

Ruby 红宝石

Ruby 1.9 has a default JSON gem with C extensions. Ruby 1.9有一个带C扩展的默认JSON gem You can use it with 你可以用它

require 'json'
JSON.parse ''{ "x": "y" }'
# => {"x"=>"y"}

The parse! parse! variant can be used for safe sources. 变体可用于安全来源。 There are also other gems, which may be faster than the default implementation. 还有其他宝石,可能比默认实现更快。 Please refer to multi_json for the list. 请参阅multi_json以获取列表。

Rails 轨道

Modern versions of Rails use multi_json , a gem that automatically uses the fastest JSON gem available. Rails的现代版本使用multi_json ,这是一个自动使用最快的JSON gem的gem。 Thus, the recommended way is to use 因此,推荐的方法是使用

object = ActiveSupport::JSON.decode json_string

Please refer to ActiveSupport::JSON for more information. 有关更多信息,请参阅ActiveSupport :: JSON In particular, the important line in the method source is 特别是,方法源中的重要一行是

data = MultiJson.load(json, options)

Then in your Gemfile, include the gems you want to use. 然后在您的Gemfile中,包含您要使用的gem。 For example, 例如,

group :production do
  gem 'oj'
end

#6楼

Parsing JSON in Rails is quite straightforward: 在Rails中解析JSON非常简单:

parsed_json = ActiveSupport::JSON.decode(your_json_string)

Let's suppose, the object you want to associate the shortUrl with is a Site object, which has two attributes - short_url and long_url. 假设,您想要与shortUrl关联的对象是Site对象,它有两个属性 - short_url和long_url。 Than, to get the shortUrl and associate it with the appropriate Site object, you can do something like: 要获得shortUrl并将其与相应的Site对象相关联,您可以执行以下操作:

parsed_json["results"].each do |longUrl, convertedUrl|
  site = Site.find_by_long_url(longUrl)
  site.short_url = convertedUrl["shortUrl"]
  site.save
end
 类似资料: