根据http://jsonlint.com/但是tidyjson对象:
library(dplyr)
library(tidyjson)
json <- '
[{"country":"us","city":"Portland","topics":[{"urlkey":"videogame","name":"Video Games","id":4471},{"urlkey":"board-games","name":"Board Games","id":19585},{"urlkey":"computer-programming","name":"Computer programming","id":48471},{"urlkey":"opensource","name":"Open Source","id":563}],"joined":1416349237000,"link":"http://www.meetup.com/members/156440062","bio":"Analytics engineer. Primarily work in the Hadoop space.","lon":-122.65,"other_services":{},"name":"Aaron Wirick","visited":1443078098000,"self":{"common":{}},"id":156440062,"state":"OR","lat":45.56,"status":"active"}]
'
json %>% as.tbl_json %>% gather_keys
我得到:
Error in gather_keys(.) : 1 records are values not objects
正如其中一条注释中所提到的,gather_keys
正在寻找对象,其中有一个数组。您可能应该在这里使用gather_array
。
此外,另一个答案使用更暴力的方法来解析tidyjson包创建的JSON属性。如果需要的话,tidyjson提供了在更干净的管道中处理此问题的方法:
library(dplyr)
library(tidyjson)
json <- '
[{"country":"us","city":"Portland"
,"topics":[
{"urlkey":"videogame","name":"Video Games","id":4471}
,{"urlkey":"board-games","name":"Board Games","id":19585}
,{"urlkey":"computer-programming","name":"Computer programming","id":48471}
,{"urlkey":"opensource","name":"Open Source","id":563}
]
,"joined":1416349237000
,"link":"http://www.meetup.com/members/156440062"
,"bio":"Analytics engineer. Primarily work in the Hadoop space."
,"lon":-122.65,"other_services":{}
,"name":"Aaron Wirick","visited":1443078098000
,"self":{"common":{}}
,"id":156440062,"state":"OR","lat":45.56,"status":"active"
}]
'
mydf <- json %>% as.tbl_json %>% gather_array %>%
spread_values(
country=jstring('country')
, city=jstring('city')
, joined=jnumber('joined')
, bio=jstring('bio')
) %>%
enter_object('topics') %>%
gather_array %>%
spread_values(urlkey=jstring('urlkey'))
如果数组中有多个这样的对象,那么这个管道真的会发光。希望这是有帮助的,即使在事实发生很久之后!
as.tbl_json
生成的对象在我看来有点奇怪,其中一个项目的名称为document.id
,值为1。在其属性中有一个称为JSON
:
json <- '
[{"country":"us","city":"Portland","topics":[{"urlkey":"videogame","name":"Video Games","id":4471},{"urlkey":"board-games","name":"Board Games","id":19585},{"urlkey":"computer-programming","name":"Computer programming","id":48471},{"urlkey":"opensource","name":"Open Source","id":563}],"joined":1416349237000,"link":"http://www.meetup.com/members/156440062","bio":"Analytics engineer. Primarily work in the Hadoop space.","lon":-122.65,"other_services":{},"name":"Aaron Wirick","visited":1443078098000,"self":{"common":{}},"id":156440062,"state":"OR","lat":45.56,"status":"active"}]
'
obj <- json %>% as.tbl_json
> dput(obj)
structure(list(document.id = 1L), .Names = "document.id", row.names = 1L, class = c("tbl_json",
"tbl", "data.frame"), JSON = list(list(structure(list(country = "us",
city = "Portland", topics = list(structure(list(urlkey = "videogame",
name = "Video Games", id = 4471L), .Names = c("urlkey",
"name", "id")), structure(list(urlkey = "board-games", name = "Board Games",
id = 19585L), .Names = c("urlkey", "name", "id")), structure(list(
urlkey = "computer-programming", name = "Computer programming",
id = 48471L), .Names = c("urlkey", "name", "id")), structure(list(
urlkey = "opensource", name = "Open Source", id = 563L), .Names = c("urlkey",
"name", "id"))), joined = 1416349237000, link = "http://www.meetup.com/members/156440062",
bio = "Analytics engineer. Primarily work in the Hadoop space.",
lon = -122.65, other_services = structure(list(), .Names = character(0)),
name = "Aaron Wirick", visited = 1443078098000, self = structure(list(
common = structure(list(), .Names = character(0))), .Names = "common"),
id = 156440062L, state = "OR", lat = 45.56, status = "active"), .Names = c("country",
"city", "topics", "joined", "link", "bio", "lon", "other_services",
"name", "visited", "self", "id", "state", "lat", "status")))))
从中可以看出,为了获得嵌入列表对象的名称,即该属性的值,您需要这样做:
names( attr(obj, "JSON")[[1]][[1]] )
#------------
[1] "country" "city" "topics" "joined" "link"
[6] "bio" "lon" "other_services" "name" "visited"
[11] "self" "id" "state" "lat" "status"
希望我能提供更多帮助,但至少你明白错误的来源。(我也希望在该软件包的帮助页面上有更多的例子。)