【问题标题】:Rails parsing jsonRails 解析 json
【发布时间】:2015-07-20 17:47:10
【问题描述】:

请求一些 API 后,我得到一个 json 哈希。我想要的是遍历该哈希以获取标签,但我得到no implicit conversion of String into Integer。请问有什么帮助或更好的方法吗?

{
  "status": "OK",
  "result": [
   {
    "verdict": "OK",
    "problem": {
      "tags": [
        "implementation", ...
      ]
    }
   },  
   .
   .
  ]
}

模型/codeforces_api.rb

def self.retrieve_results(current_user)

    json = JSON.parse(open("http://codeforces.com/api/user.status?handle=#{current_user.code_forces_handle}&key='my-key'&secret='my-secret'").read).to_json     

    ActiveSupport::JSON.decode(json)["result"].each do |record|
       if record["verdict"] == "OK"
          record ["problem"].each do |problem|
             problem ["tags"].each do |tag|
                puts tag
             end              
           end
       end
    end
end

【问题讨论】:

    标签: ruby-on-rails ruby json


    【解决方案1】:

    看起来像你想要的:

    record["problem"]["tags"].each do |tag|
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多