繁体   English   中英

Ruby:检查JSON和命名变量

[英]Ruby: Checking JSON and Naming Variables

我真的是在Ruby中使用JSON的新手,并且很难弄清楚为什么我正在运行的脚本会生成空白行。 假设我正在解析一个文件,该文件并不总是存在一个元素。 我想检查该元素是否存在,并以特定方式取决于结果名称变量。 这是我一直在尝试的

require 'rubygems'
require 'json'

file = File.open("/path/to/file.json", encoding: 'UTF-8')
json = file.read
data = JSON.parse(json)

if data["snapshots"][-1]["responses"][2].nil?
  item = something
elseif
  item = something else
end

puts item

这不会产生错误,而只是产生空白行。 我确定我在做一些明显的错误,但是希望能对您有所帮助。 谢谢!

您的主要问题是您拥有elseif而不是elsif

使用下面提供的JSON的完整代码:

require 'json'

json = <<EOS
{
 "snapshots": [
    {
        "steps": 10,
        "responses": [
            {
                "tokens": [
                    "Answer"
                ],
                "questionPrompt": "Question1?"
            },
            {
                "tokens": [
                    "Answer"
                ],
                "questionPrompt": "Question2?"
            },
            {
                "locationResponse": {
                    "location": {
                        "speed": 0,
                        "timestamp": "2014-04-20T17: 28: 37-0400",
                        "longitude": "-xx.xxxxxxx",
                        "latitude": "xx.xxxxxx",
                        "verticalAccuracy": 3,
                        "course": 0,
                        "horizontalAccuracy": 5
                    },
                    "text": "Response"
                },
                "questionPrompt": "Question3?"
            },
            {
                "tokens": [
                    "Answer"
                ],
                "questionPrompt": "Question4?"
            }
        ],
        "battery": 0.75,
        "sectionIdentifier": "1-2014-5-7",
        "audio": {
            "avg": -49.84988,
            "peak": -39.73056
        },
        "background": 0,
        "date": "2014-05-07T23: 20: 57-0400",
        "location": {
            "speed": -1,
            "placemark": {
                "subAdministrativeArea": "County",
                "subLocality": "CityName",
                "thoroughfare": "Street",
                "administrativeArea": "xx",
                "subThoroughfare": "xxx",
                "postalCode": "xxxxx",
                "region": "<+xx.xxxxxx",
                "radius": 28.13,
                "country": "UnitedStates",
                "locality": "CityName",
                "name": "Address"
            },
            "timestamp": "2014-05-07T23: 20: 58-0400",
            "longitude": "-xx.xxxxxxx",
            "latitude": "xx.xxxxxxx",
            "verticalAccuracy": 10,
            "course": 0,
            "horizontalAccuracy": 65
        },
        "dwellStatus": 0,
        "weather": {
            "relativeHumidity": "68%",
            "visibilityKM": 16.1,
            "tempC": 13.3,
            "precipTodayIn": 0,
            "windKPH": 0,
            "latitude": 40.813984,
            "windDegrees": 159,
            "stationID": "xxxxxxxx",
            "visibilityMi": 10,
            "pressureIn": 30.2,
            "pressureMb": 1023,
            "feelslikeF": 55.9,
            "windGustKPH": 12.4,
            "longitude": -77.895775,
            "feelslikeC": 13.3,
            "precipTodayMetric": 0,
            "tempF": 55.9,
            "windDirection": "SSE",
            "dewpointC": 8,
            "uv": 0,
            "weather": "Overcast",
            "windGustMPH": 7.7,
            "windMPH": 0
        },
        "connection": 1,
        "sync": 0,
        "reportImpetus": 0,
        "draft": 0
    }
   ]
}
EOS

data = JSON.parse(json)

if data["snapshots"][-1]["responses"][2].nil?
 item = "was nil"
elsif # THIS WAS elseif before
 item = "NOT nil"
end

puts item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM