我正在尝试在json输出中实现此结果:
[
{
"source": {
"v1": "mickey"
},
"target": {
"v2": "mouse"
}
},
{
"source": {
"v1": "donald"
},
"target": {
"v2": "duck"
}
}
]
为此,我写了这个:
library(dplyr)
library(jsonlite)
v1 = c("mickey","donald")
v2 = c("mouse","duck")
v = tbl_df(cbind(v1,v2))
edges <- lapply(1:nrow(v),FUN=function(i){
list(list(source=v[i,'v1'],target=v[i,'v2']))
})
write(toJSON(edges, pretty = TRUE), "disney.json")
由此我无法找到自定义括号的方法,因为目前这是我的输出:
[
[
{
"source": [
{
"v1": "mickey"
}
],
"target": [
{
"v2": "mouse"
}
]
}
],
[
{
"source": [
{
"v1": "donald"
}
],
"target": [
{
"v2": "duck"
}
]
}
]
]
那么,这是一种获得嵌套json的好/有效方法?我确实在
之前看了How to write to json with children from R和Trying to turn a data frame into hierarchical json array using jsonlite in r答案 0 :(得分:0)
您是否尝试在 singleton 元素上使用 soFar.pop();
?
显然,这会将 scalar 属性附加到元素,并可能阻止它在您的 jsonlite::unbox()
调用中被序列化为数组。