1
我使用ruby從Google geocode API獲取一些地址信息。我得到JSON返回並解析這個變量是一個包含哈希的數組。使用ruby輕鬆訪問陣列中的哈希值
我解析的JSON之後,它看起來像這樣
{"status"=>"OK",
"results"=>
[{"address_components"=>
[{"long_name"=>"XXX", "types"=>["street_number"], "short_name"=>"XXX"},
{"long_name"=>"St Georges Terrace",
"types"=>["route"],
"short_name"=>"St Georges Terrace"},
{"long_name"=>"Perth",
"types"=>["locality", "political"],
"short_name"=>"Perth"},
{"long_name"=>"Western Australia",
"types"=>["administrative_area_level_1", "political"],
"short_name"=>"WA"},
{"long_name"=>"Australia",
"types"=>["country", "political"],
"short_name"=>"AU"},
{"long_name"=>"6000", "types"=>["postal_code"], "short_name"=>"6000"}],
"types"=>["street_address"],
"geometry"=>
{"location_type"=>"ROOFTOP",
"viewport"=>
{"northeast"=>{"lng"=>115.86768092068, "lat"=>-31.9540383793198},
"southwest"=>{"lng"=>115.86138567932, "lat"=>-31.9603336206802}},
"location"=>{"lng"=>115.8645333, "lat"=>-31.957186}},
"formatted_address"=>"XXX St Georges Terrace, Perth WA 6000, Australia"}]}
我的紅寶石看起來像這樣
require 'rubygems'
require 'json'
require 'open-uri'
require 'pp'
@url = "http://maps.googleapis.com/maps/api/geocode/json?address=PERTH+XXX+St+Georges+Terrace,+Western+Australia&sensor=false"
uri = URI.parse(@url)
json = uri.open.read
parsed_json = JSON.parse(json)
pp parsed_json
hashjes和數組的混合物困惑我的紅寶石。我希望將信息提取到一個散列中,看起來像這樣
result = {「address_line_one」=>「XXX St Georges Terrace」,「address_line_two」=>「Perth」,「state」=>「Western Austalia 」, 「POSTAL_CODE」=> 「6000」}
由於