我成功拨打凤凰城的API。但是我很难接受回应并以我喜欢的方式展示它。以下是我的原始回复:
%{"date" => "2016-11-22", "games" => [%{"away" => %{"alias" => "POR", "id" => "583ed056-fb46-11e1-82cb-f4ce4684ea4c", "name" => "Portland Trail Blazers"}, "away_points" => 103, "broadcast" => %{"network" => "MSG", "satellite" => "634"}, "coverage" => "full", "home" => %{"alias" => "NYK", "id" => "583ec70e-fb46-11e1-82cb-f4ce4684ea4c", "name" => "New York Knicks"}, "home_points" => 107, "id" => "c58e2153-95f4-46b5-b10d-f89d342e4f9c", "scheduled" => "2016-11-23T00:30:00+00:00", "status" => "closed", "venue" => %{"address" => "Four Pennsylvania Plaza", "capacity" => 19812, "city" => "New York", "country" => "USA", "id" => "583152aa-de75-5bea-ac92-ac5b8a51f9f9", "name" => "Madison Square Garden", "state" => "NY", "zip" => "10001"}}, %{"away" => %{"alias" => "NOP", "id" => "583ecc9a-fb46-11e1-82cb-f4ce4684ea4c", "name" => "New Orleans Pelicans"}, "away_points" => 112, "broadcast" => %{"network" => "NBA TV", "satellite" => "216"}, "coverage" => "full", "home" => %{"alias" => "ATL", "id" => "583ecb8f-fb46-11e1-82cb-f4ce4684ea4c", "name" => "Atlanta Hawks"}, "home_points" => 94, "id" => "fdae2288-e112-4bee-a028-16b761818d2f", "scheduled" => "2016-11-23T00:30:00+00:00", "status" => "closed", "venue" => %{"address" => "One Philips Drive", "capacity" => 18118, "city" => "Atlanta", "country" => "USA", "id" => "fd21f639-8a47-51ac-a5dd-590629d445cf", "name" => "Philips Arena", "state" => "GA", "zip" => "30303"}}, %{"away" => %{"alias" => "CHI", "id" => "583ec5fd-fb46-11e1-82cb-f4ce4684ea4c", "name" => "Chicago Bulls"}, "away_points" => 107, "broadcast" => %{"network" => "ALT", "satellite" => "681"}, "coverage" => "full", "home" => %{"alias" => "DEN", "id" => "583ed102-fb46-11e1-82cb-f4ce4684ea4c", "name" => "Denver Nuggets"}, "home_points" => 110, "id" => "cd1bace5-3cdd-46b4-b175-7475bebcf050", "scheduled" => "2016-11-23T02:00:00+00:00", "status" => "closed", "venue" => %{"address" => "1000 Chopper Circle", "capacity" => 19155, "city" => "Denver", "country" => "USA", "id" => "1a28ef88-76c9-5bcc-b4ee-51d30ca98f4f", "name" => "Pepsi Center", "state" => "CO", "zip" => "80204"}}, %{"away" => %{"alias" => "OKC", "id" => "583ecfff-fb46-11e1-82cb-f4ce4684ea4c", "name" => "Oklahoma City Thunder"}, "away_points" => 109, "broadcast" => %{"network" => "NBA TV", "satellite" => "216"}, "coverage" => "full", "home" => %{"alias" => "LAL", "id" => "583ecae2-fb46-11e1-82cb-f4ce4684ea4c", "name" => "Los Angeles Lakers"}, "home_points" => 111, "id" => "ed4bf9d1-3d12-4398-9340-6ba9a56c5e4a", "scheduled" => "2016-11-23T03:30:00+00:00", "status" => "closed", "venue" => %{"address" => "1111 S. Figueroa St.", "capacity" => 19060, "city" => "Los Angeles", "country" => "USA", "id" => "792ec100-691e-5e16-8ef8-79b2b6ee38ba", "name" => "Staples Center", "state" => "CA", "zip" => "90015"}}], "league" => %{"alias" => "NBA", "id" => "4353138d-4c22-4396-95d8-5f587d2df25c", "name" => "NBA"}}
<%= for schedule <- @schedule do %>
<tr>
<td><%= schedule.games %></td>
<td></td>
</tr>
<% end %>
argument error
控制器:
def list_schedules(conn, params) do
date = build_date(params["list_schedules"]["date"])
with {:ok, schedule} <- GameSchedules.get_schedule(date) do
render(conn, "index.html", schedule: schedule)
end
end
def get_schedule(date) do
url = url_from_date(date)
case HTTPoison.get(url) do
{:ok, %{status_code: 200, body: body}} ->
{:ok, Poison.decode!(body)}
{:ok, %{status_code: 404}} ->
{:error, :not_found}
{:error, _err} ->
{:error, :internal_server_error}
end
end
defp url_from_date(date) do
"https://api.sportradar.us/nba/trial/v4/en/games/#{date}/schedule.json?api_key=zknxtpcs4d9j3yw93e5aqkjk"
end
如何在视图中显示响应并显示我想要的内容?
我希望迭代游戏并返回在特定夜晚玩的球队的名字。
答案 0 :(得分:1)
@schedule
这是一张地图,而不是一张清单。如果要打印有关每个游戏的信息,您应该迭代@schedule["games"]
。以下是如何打印Home and Away团队的方法。 <td>
内的两个<tr>
中的名称:
<%= for game <- @schedule["games"] do %>
<tr>
<td><%= game["away"]["name"] %></td>
<td><%= game["home"]["name"] %></td>
</tr>
<% end %>