首页 > 解决方案 > 使用 jsonlite 从 R 中的福布斯网站获取 json 时出错

问题描述

这是我的代码:

forbesList<-fromJSON('https://www.forbes.com/ajax/list/data?year=2018&uri=powerful-brands&type=organization')

错误详情:

parse_con(txt, bigint_as_char) 中的错误:
  词法错误:json 文本中的字符无效。
           (!doctype html) (html lang="en")
(就在这里)------^

请帮帮我,我尝试了很多方法来解决这个问题,但都失败了。任何帮助深表感谢。

标签: rjsonlite

解决方案


的第一个参数fromJSON需要是实际的 JSON 文本,而不是指向某些 JSON 文本的 URL。尝试先下载 JSON 内容,然后拨打电话fromJSON

library(httr)

url <- "https://www.forbes.com/ajax/list/data?year=2018&uri=powerful-brands&type=organization"
req <- GET(url)
stop_for_status(req)
json <- content(req, "text")
forbesList <- fromJSON(json)

我在这里验证了您的 URL 中的 JSON 内容解析正确,所以我认为这应该不是问题。


推荐阅读