从大型地图中提取特定值

时间:2017-11-07 02:35:09

标签: api elixir phoenix-framework

我有一张非常大的地图,我试图从中获取数据,但我不确定如何访问我想要的值。

MAP

 %{"attendance" => 20562,
  "away" => %{"id" => "583ecb8f-fb46-11e1-82cb-f4ce4684ea4c",
    "leaders" => %{"assists" => [%{"full_name" => "Dennis Schroder",
         "id" => "a2c6a907-282f-4172-9d60-42d03987da0e",
         "jersey_number" => "17", "position" => "G", "primary_position" => "PG",
         "statistics" => %{"assists" => 6, "assists_turnover_ratio" => 1.5,
           "blocked_att" => 1, "blocks" => 0, "defensive_rebounds" => 1,
           "field_goals_att" => 16, "field_goals_made" => 10,
           "field_goals_pct" => 62.5, "flagrant_fouls" => 0,
           "free_throws_att" => 5, "free_throws_made" => 5,
           "free_throws_pct" => 100.0, "minutes" => "32:09",
           "offensive_rebounds" => 1, "personal_fouls" => 3, "pls_min" => 3,
           "points" => 28, "rebounds" => 2, "steals" => 0, "tech_fouls" => 1,
           "three_points_att" => 6, "three_points_made" => 3,
           "three_points_pct" => 50.0, "turnovers" => 4, "two_points_att" => 10,
           "two_points_made" => 7, "two_points_pct" => 70.0}}],
      "points" => [%{"full_name" => "Dennis Schroder",
         "id" => "a2c6a907-282f-4172-9d60-42d03987da0e",
         "jersey_number" => "17", "position" => "G", "primary_position" => "PG",
         "statistics" => %{"assists" => 6, "assists_turnover_ratio" => 1.5,
           "blocked_att" => 1, "blocks" => 0, "defensive_rebounds" => 1,
           "field_goals_att" => 16, "field_goals_made" => 10,
           "field_goals_pct" => 62.5, "flagrant_fouls" => 0,
           "free_throws_att" => 5, "free_throws_made" => 5,
           "free_throws_pct" => 100.0, "minutes" => "32:09",
           "offensive_rebounds" => 1, "personal_fouls" => 3, "pls_min" => 3,
           "points" => 28, "rebounds" => 2, "steals" => 0, "tech_fouls" => 1,
           "three_points_att" => 6, "three_points_made" => 3,
           "three_points_pct" => 50.0, "turnovers" => 4, "two_points_att" => 10,
           "two_points_made" => 7, "two_points_pct" => 70.0}}],
      "rebounds" => [%{"full_name" => "Dwight Howard",
         "id" => "ae8df356-5e57-43e7-be4d-5ec7663312bd", "jersey_number" => "8",
         "position" => "C", "primary_position" => "C",
         "statistics" => %{"assists" => 2, "assists_turnover_ratio" => 0.67,
           "blocked_att" => 0, "blocks" => 3, "defensive_rebounds" => 15,
           "field_goals_att" => 5, "field_goals_made" => 3,
           "field_goals_pct" => 60.0, "flagrant_fouls" => 0,
           "free_throws_att" => 4, "free_throws_made" => 1,
           "free_throws_pct" => 25.0, "minutes" => "26:59",
           "offensive_rebounds" => 2, "personal_fouls" => 4, "pls_min" => 3,
           "points" => 7, "rebounds" => 17, "steals" => 0, "tech_fouls" => 0,
           "three_points_att" => 0, "three_points_made" => 0,
           "three_points_pct" => 0.0, "turnovers" => 3, "two_points_att" => 5,
           "two_points_made" => 3, "two_points_pct" => 60.0}}]},
    "market" => "Atlanta", "name" => "Hawks", "points" => 110,
    "scoring" => [%{"number" => 1, "points" => 20, "sequence" => 1,
       "type" => "quarter"},
     %{"number" => 2, "points" => 33, "sequence" => 2, "type" => "quarter"},
     %{"number" => 3, "points" => 31, "sequence" => 3, "type" => "quarter"},
     %{"number" => 4, "points" => 26, "sequence" => 4, "type" => "quarter"}]},
  "clock" => "00:00", "coverage" => "full", "duration" => "2:16",
  "home" => %{"id" => "583ec773-fb46-11e1-82cb-f4ce4684ea4c",
    "leaders" => %{"assists" => [%{"full_name" => "LeBron James",
         "id" => "0afbe608-940a-4d5d-a1f7-468718c67d91",
         "jersey_number" => "23", "position" => "F", "primary_position" => "SF",
         "statistics" => %{"assists" => 5, "assists_turnover_ratio" => 5.0,
           "blocked_att" => 1, "blocks" => 0, "defensive_rebounds" => 7,
           "field_goals_att" => 17, "field_goals_made" => 8,
           "field_goals_pct" => 47.1, "flagrant_fouls" => 0,
           "free_throws_att" => 5, "free_throws_made" => 5,
           "free_throws_pct" => 100.0, "minutes" => "36:52",
           "offensive_rebounds" => 2, "personal_fouls" => 3, "pls_min" => 7,
           "points" => 23, "rebounds" => 9, "steals" => 3, "tech_fouls" => 0,
           "three_points_att" => 5, "three_points_made" => 2,
           "three_points_pct" => 40.0, "turnovers" => 1, "two_points_att" => 12,
           "two_points_made" => 6, "two_points_pct" => 50.0}}],
      "points" => [%{"full_name" => "Kyrie Irving",
         "id" => "dd146010-902b-4ad7-b98c-650d0363a2f0", "jersey_number" => "2",
         "position" => "G", "primary_position" => "PG",
         "statistics" => %{"assists" => 4, "assists_turnover_ratio" => 0,
           "blocked_att" => 0, "blocks" => 1, "defensive_rebounds" => 1,
           "field_goals_att" => 27, "field_goals_made" => 11,
           "field_goals_pct" => 40.7, "flagrant_fouls" => 0,
           "free_throws_att" => 7, "free_throws_made" => 6,
           "free_throws_pct" => 85.7, "minutes" => "39:28",
           "offensive_rebounds" => 0, "personal_fouls" => 3, "pls_min" => -4,
           "points" => 29, "rebounds" => 1, "steals" => 0, "tech_fouls" => 0,
           "three_points_att" => 7, "three_points_made" => 1,
           "three_points_pct" => 14.3, "turnovers" => 0, "two_points_att" => 20,
           "two_points_made" => 10, "two_points_pct" => 50.0}}],
      "rebounds" => [%{"full_name" => "Kevin Love",
         "id" => "7d615ccd-db96-42a3-9a6c-7f18ea25634e", "jersey_number" => "0",
         "position" => "F", "primary_position" => "PF",
         "statistics" => %{"assists" => 3, "assists_turnover_ratio" => 1.5,
           "blocked_att" => 2, "blocks" => 0, "defensive_rebounds" => 6,
           "field_goals_att" => 17, "field_goals_made" => 7,
           "field_goals_pct" => 41.2, "flagrant_fouls" => 0,
           "free_throws_att" => 11, "free_throws_made" => 10,
           "free_throws_pct" => 90.9, "minutes" => "38:10",
           "offensive_rebounds" => 6, "personal_fouls" => 4, "pls_min" => -8,
           "points" => 24, "rebounds" => 12, "steals" => 3, "tech_fouls" => 0,
           "three_points_att" => 4, "three_points_made" => 0,
           "three_points_pct" => 0.0, "turnovers" => 2, "two_points_att" => 13,
           "two_points_made" => 7, "two_points_pct" => 53.846}}]},
    "market" => "Cleveland", "name" => "Cavaliers", "points" => 106,
    "scoring" => [%{"number" => 1, "points" => 20, "sequence" => 1,
       "type" => "quarter"},
     %{"number" => 2, "points" => 22, "sequence" => 2, "type" => "quarter"},
     %{"number" => 3, "points" => 37, "sequence" => 3, "type" => "quarter"},
     %{"number" => 4, "points" => 27, "sequence" => 4, "type" => "quarter"}]},
  "id" => "35a64e45-75f1-4d7e-a855-37e9051733fc", "lead_changes" => 12,
  "neutral_site" => false, "quarter" => 4,
  "scheduled" => "2016-11-09T00:00:00+00:00", "status" => "closed",
  "times_tied" => 7}

如果你花时间阅读这张地图,我试图让#34;领导者"从它的全名vaule。这是我尝试过的失败。

  <%= for boxscore <- @boxscore do %>
    <td><%= boxscore["away"]["leaders"]["points"] %></td>
  <% end %>

返回此错误:

no function clause matching in Access.get/3

我怎样才能从这张地图中提取领导者值?

1 个答案:

答案 0 :(得分:1)

此地图是@boxscore还是boxscore?我在IEx中进行了测试,它确实很好,我的意思是 - boxscore["away"]["leaders"]["points"]做了预期的事情:

[%{"full_name" => "Dennis Schroder",
   "id" => "a2c6a907-282f-4172-9d60-42d03987da0e", "jersey_number" => "17",
   "position" => "G", "primary_position" => "PG",
   "statistics" => %{"assists" => 6, "assists_turnover_ratio" => 1.5,
     "blocked_att" => 1, "blocks" => 0, "defensive_rebounds" => 1,
     "field_goals_att" => 16, "field_goals_made" => 10,
     "field_goals_pct" => 62.5, "flagrant_fouls" => 0, "free_throws_att" => 5,
     "free_throws_made" => 5, "free_throws_pct" => 100.0, "minutes" => "32:09",
     "offensive_rebounds" => 1, "personal_fouls" => 3, "pls_min" => 3,
     "points" => 28, "rebounds" => 2, "steals" => 0, "tech_fouls" => 1,
     "three_points_att" => 6, "three_points_made" => 3,
     "three_points_pct" => 50.0, "turnovers" => 4, "two_points_att" => 10,
     "two_points_made" => 7, "two_points_pct" => 70.0}}]

也许您正在尝试浏览@boxscore,它作为地图而不是列表。因此,您迭代{key, value}对,所以

for boxscore <- @boxscore do
  boxscore["away"]["leaders"]["points"]
end

将返回** (FunctionClauseError) no function clause matching in Access.get/3,因为例如。 @boxscore["attendance"]是一个整数,因此Access.get/3将找不到该情况的子句并从上面返回错误。

只需尝试访问@boxscore["away"]["leaders"]["points"] - 甚至更好 - 使用Kernel.get_in/2

get_in(@boxscore, ["away", "leaders", "points"])