从googlemaps JSON响应中提取值

ted*_*lay -1 regex erlang elixir

在此输入图像描述 来自googlemap API的我的JSON_Respon给出了

%{ body: body} = HTTPoison.get! url


body = {
   "geocoded_waypoints" : [{ ... },{ ... }],
   "routes" : [{
         "bounds" : { ...},
         "copyrights" : "Map data ©2018 Google",
         "legs" : [
        {
               "distance" : {
                  "text" : "189 km",
                  "value" : 188507
               },
               "duration" : {
                  "text" : "2 hours 14 mins",
                  "value" : 8044
               },
               "end_address" : "Juhan Liivi 2, 50409 Tartu, Estonia",
               "end_location" : {
                  "lat" : 58.3785389,
                  "lng" : 26.7146963
               },
               "start_address" : "J. Sütiste tee 44, 13420 Tallinn, Estonia",
               "start_location" : {
                  "lat" : 59.39577569999999,
                  "lng" : 24.6861104
               },
               "steps" : [
                  { ... },
                  { ... },
                  { ... },
                  { ... },
                  {
                     "distance" : {
                        "text" : "0.9 km",
                        "value" : 867
                     },
                     "duration" : {
                        "text" : "2 mins",
                        "value" : 104
                     },
                     "end_location" : {
                        "lat" : 59.4019886,
                        "lng" : 24.7108114
                     },
                     "html_instructions" : "XXXX",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "XXXX"
                     },
                     "start_location" : {
                        "lat" : 59.3943677,
                        "lng" : 24.708647
                     },
                     "travel_mode" : "DRIVING"
                  },
                  { ... },
                  { ... },
                  { ... },
                  { ... },
                  { ... },
                  { ... },
                  { ... },
                  { ... },
                  { ... }
               ],
               "traffic_speed_entry" : [],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : { ... },
         "summary" : "Tallinn–Tartu–Võru–Luhamaa/Route 2",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}
Run Code Online (Sandbox Code Playgroud)

使用来自Regex.named_captures模块的bellow命令,用红色检查所附的图像

%{"duration_text" => duration_text, "duration_value" => duration_value} = Regex.named_captures ~r/duration\D+(?<duration_text>\d+ mins)\D+(?<duration_value>\d+)/, body
Run Code Online (Sandbox Code Playgroud)

在bleu中(查看附图),我想从身体中提取的是我在浏览器上的googleAPI网址的JSON响应

你能协助并提供正则表达式吗?

由于http://www.elixre.uk/已关闭,我无法找到任何帮助这样做的api

提前致谢

7st*_*tud 5

不要在json字符串上使用正则表达式.相反,使用Jason,Poison等将json字符串转换为elixir地图,然后使用地图中的键来查找您感兴趣的数据.

这是一个例子:

json_map = Jason.decode!(get_json())

[first_route | _rest] = json_map["routes"]
[first_leg |  _rest] = first_route["legs"]
distance = first_leg["distance"]

=> %{"text" => "189 km", "value" => 188507}
Run Code Online (Sandbox Code Playgroud)

同样,您可以通过以下方式获取其他部分:

duration = first_leg["duration"]
end_address = first_leg["end_address"]
...
...
Run Code Online (Sandbox Code Playgroud)