Ruby中的JSON解析 [英] JSON Parsing in Ruby

查看:110
本文介绍了Ruby中的JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Ruby中的JSON(使用JSON gem)解析以下内容:

I'm trying to parse the below from JSON (using the JSON gem) in Ruby:

{ 
  "daily": {
    "summary":"Light rain today through Saturday, with temperatures rising to 88°F on Saturday.",
    "icon":"rain",
    "data":[
      {
        "time":1435464000,
        "precipProbability":0.99
      }
    ]
  }
}

当前我所拥有的是:forecast["daily"]["data"],但是当它是1435464000时,我想获取precipProbabilityprecipProbability.关于如何完成我当前的JSON解析查询的任何建议?

Currently what I have is this: forecast["daily"]["data"], but I want to get the precipProbability for time when it is 1435464000. Any suggestion on how to complete my current JSON parsing query?

推荐答案

最简单的查询解决方案是

The easiest solution to your query would be

forecast["daily"]["data"].first['precipProbability']

但是我建议您将其放入循环内,因为阵列上可能没有数据或有一个以上数据

However I suggest you make it inside a loop as there might be no data or more than one data on the array

forecast["daily"]["data"].each do |data|
  puts data['precipProbability'] # or your implementation logic 
end

这篇关于Ruby中的JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆