2016-07-22 60 views
-1

當我在Ruby中解析JSON文件時,我很難獲取特定值。我的JSON基於這個鏈接https://www.mcdonalds.com/services/mcd/us/restaurantLocator?latitude=40.7217861&longitude=-74.00944709999999&radius=8045&maxResults=100&country=us&language=en-usJson解析中的特定值

無論我嘗試什麼,我都無法拉出我想要的值,即「addressLine1」字段。我得到以下錯誤:

require 'json' 

file = File.read('MCD.json') 
data_hash = JSON.parse(file) 


print data_hash.keys 
print "\n" 

print data_hash['features']['addressLine1'] 

回答

1

data_hash['features']是一個數組

`[]': no implicit conversion of String into Integer (TypeError) 

代碼。根據你有什麼實際需要,你可能要麼迭代它,或致電:

data_hash['features'].first['properties']['addressLine1'] 

注意'properties'那裏,因爲addressLine1不是'features'元素的直系後裔。

+0

這是有幫助的。現在只需要迭代來獲取整個文件。 – awald