【问题标题】:JSON: Google Map Distance Matrix Data in R "How to download JSON data into R?"JSON:R 中的 Google 地图距离矩阵数据“如何将 JSON 数据下载到 R 中?”
【发布时间】:2016-05-31 18:26:54
【问题描述】:

我正在尝试将数据从 Google Map API 距离矩阵获取到 R。我正在尝试将 JSON 数据获取到 R。如何将 JSON 数据下载到 R,以便稍后进行解析?

require(rjson)
url <- "https://maps.googleapis.com/maps/api/distancematrix/jsonunits=imperial&origins=19+East+34th+Street+NewYork+NY+10016&destinations=40.5177433,-74.2749576&mode=transit&language=fr-FR&key=API_KEY_HERE"
raw.data <- readLines(url, warn = "F")
rd <- fromJSON(raw.data) 

我收到此错误:

Error in fromJSON(raw.data) : unexpected character '<'

感谢您的帮助

【问题讨论】:

  • 你看过httr包吗?

标签: json r google-maps google-distancematrix-api


【解决方案1】:

碰巧我写了一个包,googleway 可以提供帮助:

library(googleway)

google_distance(origins = "19 East 34th Street NewYork NY 10016",
                destinations = list(c(40.5177433,-74.2749576)),
                key = key,
                mode = "transit",
                language = "fr")

$destination_addresses
[1] "350-356 Lawrie St, Perth Amboy, NJ 08861, États-Unis"

$origin_addresses
[1] "19 E 34th St, New York, NY 10016, États-Unis"

$rows
elements
1 51,3 km, 51305, 1 heure 27 min, 5249, OK

$status
[1] "OK"

同样,设置simplify = FALSE 以获取原始json

google_distance(origins = "19 East 34th Street NewYork NY 10016",
                destinations = list(c(40.5177433,-74.2749576)),
                key = key,
                mode = "transit",
                language = "fr",
                simplify = FALSE)


[1] "{"                                                                                           
[2] "   \"destination_addresses\" : [ \"350-356 Lawrie St, Perth Amboy, NJ 08861, États-Unis\" ],"
[3] "   \"origin_addresses\" : [ \"19 E 34th St, New York, NY 10016, États-Unis\" ],"             
[4] "   \"rows\" : ["                                                                             
[5] "      {"                                                                                     
[6] "         \"elements\" : ["                                                                   
[7] "            {"                                                                               
[8] "               \"distance\" : {"                                                             
[9] "                  \"text\" : \"51,3 km\","                                                   
[10] "                  \"value\" : 51305"                                                         
[11] "               },"                                                                           
[12] "               \"duration\" : {"                                                             
[13] "                  \"text\" : \"1 heure 27 min\","                                            
[14] "                  \"value\" : 5249"                                                          
[15] "               },"                                                                           
[16] "               \"status\" : \"OK\""                                                          
[17] "            }"                                                                               
[18] "         ]"                                                                                  
[19] "      }"                                                                                     
[20] "   ],"                                                                                       
[21] "   \"status\" : \"OK\""                                                                      
[22] "}"

(其中simplify == TRUE 使用jsonlite::fromJSON

【讨论】:

    【解决方案2】:

    我知道如何通过 JSONLite 做到这一点。我使用的是 R 的原生“fromJSON”,这导致了问题。

    rd &lt;-jsonlite::fromJSON("Google_API_CALL_IN_HERE", simplifyDataFrame = TRUE)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 2022-12-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多