lexical error: inside a string, '\' occurs before a character / Using jsonlite in R

lexical error: inside a string, '\' occurs before a character / Using jsonlite in R

最近,如果我尝试解析来自 Google 的 JSON 响应,我会收到此错误。

lexical error: inside a string, '\' occurs before a character which it may not.
      [SICGCMCIAG?KAQ@I@KBKBGDGDOJ]\IHGDMDI@K@E?IAGAGAICGEGEIGIMOS
                 (right here) ------^

在浏览器中看到的由 google 发送的字符串如下所示(我将其缩短):

[SICGCMCIAG?KAQ@I@KBKBGDGDOJ]\IHGDMDI@K@E?

这是我使用的代码

route_doc <- getURL(route)
route_response <- fromJSON(route_doc)

jsonlite 不知何故挂在这里。 我该怎么做才能解析来自 google 的响应?

稍作调查后: 如果多行条目包含反斜杠字符

,则会发生此崩溃
"polyline" : {
                    "points" : "qv{uHwlni@R?"
                 },

谢谢!

更新:我正在使用此代码从 google 获得 JSON 响应:

route_from="Köln"
route_to="Hamburg"

get_route_url<-function(origin,
destination,
avoid,mode,
alternatives,
key, 
return.call = "json")

{
  root<-"https://maps.googleapis.com/maps/api/directions/" 
  u<-paste(root, 
           return.call, 
           "?origin=", origin,
           "&destination=",destination,
           "&avoid=",avoid, 
           "&mode=", mode,
           "&alternatives=",alternatives,
           "&key=",key, 
           sep = "") 
  return(URLencode(u))
  }


route<-get_route_url(route_from,
                     route_to,
                     "highways",
                     "driving",
                     "true",MYKEY)

route_doc <- getURL(route)

route_response <- fromJSON(route_doc)

和一个错误,例如折线:

lexical error: inside a string, '\' occurs before a character which it may not.
          uHiani@@L@HBHBHBDDFDBDB`@@t@@\G"                      },    
                     (right here) ------^

Google 的 API returns JSON 中的响应,因此您实际上不需要使用 [=12] 获取 'doc' =],因为fromJSON可以直接读取JSON

所以只需在您的 route 对象上调用 fromJSON

route_response <- fromJSON(route)

并查看路线

library(googleway)
mapKey <- 'my_map_key'

df <- data.frame(polyline = route_response$routes$overview_polyline$points)

google_map(key = mapKey) %>%
  add_polylines(data = df, polyline = "polyline")