首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R: JSONlite循环问题

R: JSONlite循环问题
EN

Stack Overflow用户
提问于 2016-06-02 03:02:32
回答 2查看 478关注 0票数 1

我得到了所有的“Google Map API请求”,但当我试图循环调用和解析它时。我收到一个错误。如果我不使用循环,而是手动执行,它就会起作用。

代码语言:javascript
复制
 a <- c("1780 N Washington Ave Scranton PA 18509", "1858 Hunt Ave Bronx NY 10462", "140 N Warren St Trenton NJ 08608-1308")

#API Key need to be added to run:
 w <- c("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=19+East+34th+Street+New York+NY+10016&destinations=1780+N+Washington+Ave+Scranton+PA+18509&mode=transit&language=fr-FR&key=API_KEY_HERE",
   "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=19+East+34th+Street+New York+NY+10016&destinations=1858+Hunt+Ave+Bronx+NY+10462&mode=transit&language=fr-FR&key=API_KEY_HERE",
   "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=19+East+34th+Street+New York+NY+10016&destinations=140+N+Warren+St+Trenton+NJ+08608-1308&mode=transit&language=fr-FR&key=API_KEY_HERE")

df <- data.frame(a,w)

for (i in cpghq) {
  url <- df$w
  testdf <- jsonlite::fromJSON(url, simplifyDataFrame = TRUE)
  list <- unlist(testdf$rows)
  transit_time <- as.data.frame(t(as.data.frame(list)))
  cpghq$transit_time <- transit_time

我得到的错误是:

代码语言:javascript
复制
Error: lexical error: invalid char in json text.
                                   https://maps.googleapis.com/map
                 (right here) ------^ 
EN

回答 2

Stack Overflow用户

发布于 2016-06-06 22:25:37

我的API调用是错误的,因为"New York“有空间。我修正了使用gsub("[:space:]","+",a)的问题,而且utils::URLencode()也有作用。

构建API调用

代码语言:javascript
复制
 a <- c("1780 N Washington Ave Scranton PA 18509", "1858 Hunt Ave Bronx NY 10462", "140 N Warren St Trenton NJ 08608-1308")

  fix_address <- gsub("[[:space:]]", "+", a)

  key <- "YOUR_GOOGLE_API_KEY_HERE"
  travel_mode <- "transit"

  root <- "https://maps.googleapis.com/maps/api/distancematrix/json
  units=imperial&origins="
  api_call <- paste0(root,"350+5th+Ave+New+York+NY+10118", 
                    "&destinations=",
                    fix_address,
                    "&mode=", 
                    travel_mode,
                    "&language=en-EN",
                    "&key=", key)

我的循环问题非常简单。我没有使用lapply()

现在使用RSJSONIO::fromJSON发送对Google Map API的调用

代码语言:javascript
复制
 require("RJSONIO")
 if(verbose) cat(address,"\n")
 # Get json returns from Google
doc   <- lapply(api_call, RCurl::getURL) 
票数 1
EN

Stack Overflow用户

发布于 2016-06-28 07:35:26

正如在my other answer to you中指出的,您也可以使用my googleway为您完成这项工作。

代码语言:javascript
复制
library(googleway)

key <- "your_api_key"

a <- c("1780 N Washington Ave Scranton PA 18509", 
       "1858 Hunt Ave Bronx NY 10462", 
       "140 N Warren St Trenton NJ 08608-1308")

google_distance(origins = "350 5th Ave New York NY 10188",
                destinations = as.list(a),
                mode = "transit",
                key = key, 
                simplify = T)


# $destination_addresses
# [1] "1780 N Washington Ave, Scranton, PA 18509, USA" "1858 Hunt Ave, Bronx, NY 10462, USA"           
# [3] "140 N Warren St, Trenton, NJ 08608, USA"       
# 
# $origin_addresses
# [1] "Empire State Building, 350 5th Ave, New York, NY 10118, USA"
# 
# $rows
# elements
# 1 ZERO_RESULTS, OK, OK, NA, 19.0 km, 95.8 km, NA, 18954, 95773, NA, 54 mins, 1 hour 44 mins, NA, 3242, 6260
# 
# $status
# [1] "OK"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37576798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档