如何在R中将多个JSON文件合并为一个文件 [英] How to combine multiple JSON files into a single file in R

查看:479
本文介绍了如何在R中将多个JSON文件合并为一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个JSON文件

  • json1包含[[1,5],[5,7],[8,10]]
  • json2包含[[5,6],[4,5],[5,8]]
  • json3包含[[4,7],[3,4],[4,8]]
  • json1 contains [[1,5],[5,7],[8,10]]
  • json2 contains [[5,6],[4,5],[5,8]]
  • json3 contains [[4,7],[3,4],[4,8]]

我想将它们合并为一个文件jsonmerge:

I want to merge them into one single file jsonmerge:

  [[[1,5],[5,7],[8,10]],[[5,6],[4,5],[5,8]],[[4,7],[3,4],[4,8]]]

我尝试了连接,但是它以这种格式给出了结果

I tried concatenate but it gave results in this format

   [[5,6],[4,5],[5,8]],
   [[5,6],[4,5],[5,8]],
   [[4,7],[3,4],[4,8]]

有什么建议吗?

提前谢谢.

推荐答案

如果您使用的是rjson软件包,则需要将它们串联到一个列表中:

If you are using the rjson package, then you need to concatenate them into a list:

library(rjson)
json1 <- fromJSON(file = "json1")
json2 <- fromJSON(file = "json2")
json3 <- fromJSON(file = "json3")
jsonl <- list(json1, json2, json3)
jsonc <- toJSON(jsonc)
jsonc
[1] "[[[1,5],[5,7],[8,10]],[[5,6],[4,5],[5,8]],[[4,7],[3,4],[4,8]]]"
write(jsonc, file = "jsonc")

如果文件很多,可以将它们放入向量中,并使用lapply保存一些键入内容:

If you have many files, you can put them in a vector and use lapply to save some typing:

files <- c("json1", "json2", "json3")
jsonl <- lapply(files, function(f) fromJSON(file = f))
jsonc <- toJSON(jsonl)
write(jsonc, file = "jsonc")

这篇关于如何在R中将多个JSON文件合并为一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆