[英]HTTParty parsing JSON in Rails
I have a URL that displays JSON holding info for 400 YMCA locations. 我有一个显示400个YMCA位置的JSON持有信息的URL。 My goal is to be able to pull latitude and longitude for each location in order to display the points on a map. 我的目标是能够提取每个位置的纬度和经度,以便在地图上显示点。
I was playing around with HTTParty and just wanted to start by displaying only the latitude and longitude in the view. 我当时在玩HTTParty,只是想从在视图中仅显示纬度和经度开始。
Here is my controller: 这是我的控制器:
require 'rubygems'
require 'httparty'
class HomesController < ApplicationController
include HTTParty
def index
uri = HTTParty.get"http://www.livestrong.org/we-can-help/ymca.json"
@place = JSON.parse(uri.body)
end
end
Here is the view: 这是观点:
<% @place.each do | key, value | %> <br>
<strong><%= key %></strong>
<%= value %>
<% end %>
So far this displays all of the JSON on the screen. 到目前为止,这将在屏幕上显示所有JSON。 How do I pick out the individual parts? 如何挑选单个零件?
You have to take hints from what is displayed, if you take a look then I'm sure you'll see that your loop is only executed once, and the one and only key output is ymca
with all your data in that single value
of that key. 您必须从显示的内容中获取提示,如果您看一看,那么我确定您会看到循环仅执行一次,并且唯一且唯一的键输出是ymca
其中所有数据都在该单个value
中那把钥匙。
If you drop into irb
and take a look at what's in @place['ymca']
then you'll see it's a big array. 如果您进入irb
并查看@place['ymca']
那么您会发现它是一个很大的数组。 If you take a look at the first element of that array then you'll see it's very manageable chunk of data: 如果您看一下该数组的第一个元素,那么您将看到它是非常易于管理的数据块:
[12] pry(main)> @place['ymca'].first
=> {"phone_number"=>"205-870-0144",
"title"=>"Mountain Brook YMCA",
"street_address"=>"2401 20th Place South",
"email"=>"livestrong@ymcabham.org",
"city"=>"Birmingham",
"lat"=>33.487881,
"zipcode"=>"35223",
"description"=>"Mountain Brook YMCA",
"state"=>"Alabama",
"thumbnail"=>"v1414706252/tak5bi22hve474jeh1yr.png",
"website"=>"http://ymcabham.org",
"lon"=>-86.78624}
[13] pry(main)>
I hope you can take it from there. 我希望你能从那里拿走它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.