遍历文件夹,读取 JSON,并组合

问题描述

我有大量目录,每个目录都有一个 JSON 文件。我试图遍历这些文件夹,读取每个 JSON 文件,将它们组合成一个数据框。每个目录都有唯一的名称,但里面是同名的JSON文件

我将工作目录设置为包含我想要迭代的这些目录的目录。下面的代码在读取单个 JSON 文件时工作正常,但我似乎无法成功构建 for 循环。

setwd("'directory path'")
json_data <- jsonlite::fromJSON("./politifact11773/news content.json",flatten = T)

这一行没有任何错误jsonlite::read_jsonjsonlite::fromJSON(readLines("./politifact11773/news content.json")) 的作用相同。

现在,当我尝试获取目录名称列表、构造路径字符串并遍历它们时,我遇到了错误

dir_list <- list.dirs(recursive = F)
path <- paste0(dir_list,"/news content.json")
vec_list <- list()

for (i in 1:length(path)) {
  json_data <- jsonlite::fromJSON(path[i],flatten = TRUE)
  vec_list[i] <- c("url" = json_data$url,"title" = json_data$title,"text" = json_data$text,"top_img" = json_data$top_img,"type" = json_data$Meta_data$og$type,"site_name" = json_data$Meta_data$og$site_name,"section" = json_data$Meta_data$article$section,"publish_date" = lubridate::as_datetime(json_data$publish_date))
}
return(vec_list)

我的计划是将结果列表行绑定到数据框中。当我运行 for 循环时,出现以下错误

Error: lexical error: invalid char in json text.
                                       ./politifact13541/news content.
                     (right here) ------^
In addition: Warning messages:
1: In vec_list[i] <- c(url = json_data$url,title = json_data$title,:
  number of items to replace is not a multiple of replacement length
2: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length
3: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length
4: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length
5: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length
6: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length
7: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length
8: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length
9: In vec_list[i] <- c(url = json_data$url,:
  number of items to replace is not a multiple of replacement length

一直在谷歌搜索并尝试不同的东西,但似乎找不到突破口!

非常感谢您的帮助!

我附上了一个示例 JSON file 供您参考。由于我对 JSON 还不是很熟悉,所以我还想检查一下是否因为 JSON 格式本身。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)