我做了一个HTTPotion并收到了类似的响应。
"{\"total\": 1564, \"businesses\": [{\"review_count\": 139}, {\"review_count\": 154}]}"
这些数据是什么类型的?如何访问第一个review_count?
答案 0 :(得分:3)
这是json数据。您可以使用Poison对其进行解码
iex()> response = "{\"total\": 1564, \"businesses\": [{\"review_count\": 139}, {\"review_count\": 154}]}"
"{\"total\": 1564, \"businesses\": [{\"review_count\": 139}, {\"review_count\": 154}]}"
iex()> response |> Poison.decode! |> Map.get("businesses") |> List.first |> Map.get("review_count")
139
或更短的方式:
iex()> response |> Poison.decode! |> get_in(["businesses", Access.at(0), "review_count"])
139