将json文件转换为R中的R对象

将json文件转换为R中的R对象,json,r,rjson,Json,R,Rjson,我正在尝试将json文件转换为R格式。数据格式如下: { "id": "xyz", "root": { "author": { "name": "xyz", "email": "xyx@xyz.org", "date": "2014-10-08T00:10:30Z" }, "authorer": { "name": "xyz", "email": "xyx@xyz.org", "date": "2014-10-08T00:11:30Z" }, "message": "This a test j

我正在尝试将json文件转换为R格式。数据格式如下:

{
"id": "xyz",
"root": {
"author": {
"name": "xyz",
"email": "xyx@xyz.org",
"date": "2014-10-08T00:10:30Z"
},
"authorer": {
"name": "xyz",
"email": "xyx@xyz.org",
"date": "2014-10-08T00:11:30Z"
 },
"message": "This a test json",
"root": {
"id": "xyz1",
"url": "xyz"
},
"url": "xyz",
"message_count": 0
},
"url": "xyz",
"html_url": "xyz",
"comments_url": "abc",
"author": null,
"authorer": null,
"parent": [
{
"id": "xyz3",
"url": "xyz",
"html_url": "xyz"
}
]
}
在这之后,一个类似的行开始,以{具有相同的格式化文本}这是我在R中编写的代码

install.packages("rjson")
library(rjson)
df <- fromJSON(paste(readLines("file.json"), collapse=""))
View(df)
请参阅此处:(输入和输出的外观)

我在这里为两行提供了一个新链接:

非常感谢

这是您想要的吗:

json <- '{
  "id": "xyz",
  "root": {
    "author": {
      "name": "xyz",
      "email": "xyx@xyz.org",
      "date": "2014-10-08T00:10:30Z"
    },
    "authorer": {
      "name": "xyz",
      "email": "xyx@xyz.org",
      "date": "2014-10-08T00:11:30Z"
    },
    "message": "This a test json",
    "root": {
      "id": "xyz1",
      "url": "xyz"
    },
    "url": "xyz",
    "message_count": 0
  },
  "url": "xyz",
  "html_url": "xyz",
  "comments_url": "abc",
  "author": null,
  "authorer": null,
  "parent": [
    {
      "id": "xyz3",
      "url": "xyz",
      "html_url": "xyz"
    }
  ]
}'

out <- jsonlite::fromJSON(json)
out[vapply(out, is.null, logical(1))] <- "none"
data.frame(out, stringsAsFactors = FALSE)[,1:5]

   id root.author.name root.author.email     root.author.date root.authorer.name
1 xyz              xyz       xyx@xyz.org 2014-10-08T00:10:30Z                xyz

json您给出了格式错误的json文本和错误的预期输出!你希望我们用这个做什么?你说的格式不好是什么意思?这就是我得到文本的方式。(我从输出中删除了一些超链接,你想把它也放在文本中吗,让我知道)。我想知道是否有人在将嵌套的json结构转换为R对象。另外,您希望screen、author和root做什么?应该把它们压平吗?在发帖前试试。这将使帮助更容易。以下是带有适当括号的json文本。我想把json文本转换成R对象(列)(比如R数据集),这样我就可以分析数据了。你能发布完整的代码吗?我得到一个关于“out”部分的错误。可以在输入json中直接从文件中读取,是的,请参阅帮助
?jsonlite::fromJSON
我检查了它,但找不到它。我写了这篇文章,但它无法将它们转换为可读取的json对象。我在这里贴了一个后续问题:如果你能回答,那就太好了。谢谢
json <- '{
  "id": "xyz",
  "root": {
    "author": {
      "name": "xyz",
      "email": "xyx@xyz.org",
      "date": "2014-10-08T00:10:30Z"
    },
    "authorer": {
      "name": "xyz",
      "email": "xyx@xyz.org",
      "date": "2014-10-08T00:11:30Z"
    },
    "message": "This a test json",
    "root": {
      "id": "xyz1",
      "url": "xyz"
    },
    "url": "xyz",
    "message_count": 0
  },
  "url": "xyz",
  "html_url": "xyz",
  "comments_url": "abc",
  "author": null,
  "authorer": null,
  "parent": [
    {
      "id": "xyz3",
      "url": "xyz",
      "html_url": "xyz"
    }
  ]
}'

out <- jsonlite::fromJSON(json)
out[vapply(out, is.null, logical(1))] <- "none"
data.frame(out, stringsAsFactors = FALSE)[,1:5]

   id root.author.name root.author.email     root.author.date root.authorer.name
1 xyz              xyz       xyx@xyz.org 2014-10-08T00:10:30Z                xyz