0

我尝试在 R 中使用 API、应用程序/json,但是当我使用此代码时:


data <- fromJSON(content(XXX, type = "text"))

我看到下一个错误

Error: lexical error: invalid char in json text.
                      Producto;IdCompra;VentaN;
                     (right here) ------^

可能是什么问题?我知道我必须提取的数据是一个用“;”分隔的 csv 文件。

谢谢!

我在下面留下了我的数据的一瞥


glimpse(data)
List of 10
 $ url        : chr "https://xxx/x/x/"
 $ status_code: int 200
 $ headers    :List of 16
  ..$ server                   : chr "x"
  ..$ connection               : chr "keep-alive"
  ..$ vary                     : chr "Origin"
  ..$ vary                     : chr "Access-Control-Request-Method"
  ..$ vary                     : chr "Access-Control-Request-Headers"
  ..$ x-content-type-options   : chr "nosniff"
  ..$ x-xss-protection         : chr "1; mode=block"
  ..$ cache-control            : chr "no-cache, no-store, max-age=0, must-revalidate"
  ..$ pragma                   : chr "no-cache"
  ..$ expires                  : chr "0"
  ..$ strict-transport-security: chr "max-age=31536000 ; includeSubDomains"
  ..$ x-frame-options          : chr "DENY"
  ..$ content-type             : chr "application/json"
  ..$ content-length           : chr "402955"
  ..$ date                     : chr "Wed, 10 Nov 2021 20:18:55 GMT"
  ..$ via                      : chr "1.1 vegur"
  ..- attr(*, "class")= chr [1:2] "insensitive" "list"
 $ all_headers:List of 1
  ..$ :List of 3
  .. ..$ status : int 200
  .. ..$ version: chr "HTTP/1.1"
  .. ..$ headers:List of 16
  .. .. ..- attr(*, "class")= chr [1:2] "insensitive" "list"
 $ cookies    :'data.frame':    1 obs. of  7 variables:
  ..$ domain    : chr "#Httpxxxx.com"
  ..$ flag      : logi FALSE
  ..$ path      : chr "/"
  ..$ secure    : logi TRUE
  ..$ expiration: POSIXct[1:1], format: NA
  ..$ name      : chr "xxxxxx"
  ..$ value     : chr "xxxxxxxxxxxxx"
 $ content    : raw [1:402955] 43 61 74 65 ...
 $ date       : POSIXct[1:1], format: "2021-11-10 20:18:55"
 $ times      : Named num [1:6] 0 0.0621 0.2435 0.6197 0.8178 ...
  ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
 $ request    :List of 7
  ..$ method    : chr "GET"
  ..$ url       : chr "https://xxxx/x/x/x"
  ..$ headers   : Named chr [1:2] "application/json, text/xml, application/xml, */*" "Bearer xxxxxxxx"| __truncated__
  .. ..- attr(*, "names")= chr [1:2] "Accept" "Authorization"
  ..$ fields    : NULL
  ..$ options   :List of 3
  .. ..$ useragent: chr "libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2"
  .. ..$ cookie   : chr "=xxxxxxxxxx"
  .. ..$ httpget  : logi TRUE
  ..$ auth_token: NULL
  ..$ output    : list()
  .. ..- attr(*, "class")= chr [1:2] "write_memory" "write_function"
  ..- attr(*, "class")= chr "request"
 $ handle     :Class 'curl_handle' <externalptr> 
 - attr(*, "class")= chr "response"

4

1 回答 1

0

您尝试读取的数据不是 json 格式,因此无法使用fromJSON. 它似乎是分号分隔的格式,所以也许试试

read.table(text = content(XXX, type = "text"), sep = ";", headers = TRUE)
于 2021-11-10T21:41:40.333 回答