0

我正在尝试在R中转换一个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"
}
]
}

在此之后,类似的行开始,{具有相同的格式化文本}这是我在 R 中编写的代码

install.packages("rjson")
library(rjson)
df <- fromJSON(paste(readLines("file.json"), collapse=""))
View(df)

我想知道如何使这个文件在 R 中可读?我想将它们视为这样的列:

id  root/author/name    root/author/email   root/author/date    root/authorer/name  

参考这里: http://konklone.io/json/?id= dfeae96a607c7541b8fe(输入和输出应该是什么样子)。

我在这里为两行提供了一个新链接:http: //konklone.io/json/?id= 3b01a02e17ec4fde3357

非常感谢

4

1 回答 1

1

这是你想要的吗:

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
于 2014-11-12T19:27:02.173 回答