r - 使用 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") (就在这里)------^
请帮帮我,我尝试了很多方法来解决这个问题,但都失败了。任何帮助深表感谢。
解决方案
的第一个参数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 内容解析正确,所以我认为这应该不是问题。
推荐阅读
- css - 为什么 SCSS 代码在 VS Code 中停止工作?
- c# - 从 .NET Core 项目引用 .net Standard 程序集会导致引用错误
- java - 从文件中读取给出 java.io.StreamCorruptedException: invalid stream header: 73720027
- powershell - PowerShell 生成一个后台进程以保持活动状态
- c# - 在类中定义公共枚举有什么好处?
- reactjs - Axios 不向服务器端控制器发送标头
- python - 在 pytest 中设置动态文件夹和报告名称
- javascript - 在 Pentaho 中使用 JavaScript 循环一个 json 数组对象
- node.js - 需要一种将 response.body 数据从 express (server.js) 发送到 React (client.js) 的方法
- javascript - 将秒转换为年、月、周、日、小时、分钟和秒