最终目标是成为返回机场天气的聊天机器人的一部分. 使用import.io,我构建了一个端点来查询我提供此响应的天气服务: {"extractorData"= {"url"= "https://www.aviationweather.gov/metar/data?ids=kokbforma
使用import.io,我构建了一个端点来查询我提供此响应的天气服务:
{"extractorData"=> {"url"=> "https://www.aviationweather.gov/metar/data?ids=kokb&format=decoded&hours=0&taf=off&layout=on&date=0", "resourceId"=>"66ca907842aabb6b08b8bc12049ad533", "data"=> [{"group"=> [{"Timestamp"=>[{"text"=>"Data at: 2135 UTC 12 Dec 2016"}], "Airport"=>[{"text"=>"KOKB (Oceanside Muni, CA, US)"}], "FullText"=> [{"text"=> "KOKB 122052Z AUTO 24008KT 10SM CLR 18/13 A3006 RMK AO2 SLP179 T01780133 58021"}], "Temperature"=>[{"text"=>"17.8°C ( 64°F)"}], "Dewpoint"=>[{"text"=>"13.3°C ( 56°F) [RH = 75%]"}], "Pressure"=> [{"text"=> "30.06 inches Hg (1018.0 mb) [Sea level pressure: 1017.9 mb]"}], "Winds"=> [{"text"=>"from the WSW (240 degrees) at 9 MPH (8 knots; 4.1 m/s)"}], "Visibility"=>[{"text"=>"10 or more sm (16+ km)"}], "Ceiling"=>[{"text"=>"at least 12,000 feet AGL"}], "Clouds"=>[{"text"=>"sky clear below 12,000 feet AGL"}]}]}]}, "pageData"=> {"resourceId"=>"66ca907842aabb6b08b8bc12049ad533", "statusCode"=>200, "timestamp"=>1481578559306}, "url"=> "https://www.aviationweather.gov/metar/data?ids=kokb&format=decoded&hours=0&taf=off&layout=on&date=0", "runtimeConfigId"=>"2ddb288f-9e57-4b58-a690-1cd409f9edd3", "timestamp"=>1481579246454, "sequenceNumber"=>-1}
我似乎遇到了两个问题.我如何能:
>拉出每个字段并将其写入自己的变量
>忽略响应中的“text”修饰符.
require 'json' foo = JSON['{"a":1}'] foo # => {"a"=>1}
JSON足够聪明,可以查看参数,并根据它是字符串还是数组或哈希,解析或序列化它.在上面的例子中,它将其解析为哈希.
从那时起,普通的Ruby需要深入了解您返回的哈希并访问特定值:
foo = JSON['{"a":1, "b":[{"c":3}]}'] foo # => {"a"=>1, "b"=>[{"c"=>3}]} foo['b'][0]['c'] # => 3
如何浏览哈希在互联网和Stack Overflow上进行了广泛的讨论,因此请搜索并查看您可以找到的内容.