【问题标题】:Import multiple json files from a directory and attaching the data从一个目录导入多个 json 文件并附加数据
【发布时间】:2021-07-18 06:09:16
【问题描述】:

我正在尝试将多个 json 文件读取到工作目录中,以进一步转换为数据集。我在目录 json 中有文件 text1、text2、text3。这是我写的代码:

setwd("Users/Desktop/json")
temp = list.files(pattern="text*.")
myfiles = lapply(temp, read.delim)
library("rjson")
json_file <- "myfiles"
library(jsonlite)
out <- jsonlite::fromJSON(json_file)
out[vapply(out, is.null, logical(1))] <- "none"
data.frame(out, stringsAsFactors = FALSE)[,1:5]
View(out)

我有大约 200 个文件,所以我想知道是否可以导入 json 文件。

谢谢

【问题讨论】:

  • 看看this解决方案。

标签: json r rjson jsonlite


【解决方案1】:

我认为我在处理 Twitter 数据时遇到了类似的问题。我有一个包含每个用户名的单独文件的目录,我想将它们作为一个组导入/分析。这对我有用:

library(rjson)
filenames <- list.files("Users/Desktop/json", pattern="*.json", full.names=TRUE) # this should give you a character vector, with each file name represented by an entry
myJSON <- lapply(filenames, function(x) fromJSON(file=x)) # a list in which each element is one of your original JSON files

如果这不起作用,那么我需要更多信息来了解您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多