【发布时间】:2019-10-25 13:06:33
【问题描述】:
不是 json 专家,但我需要我认为被称为“嵌套对象”的东西,而我得到的是我认为被称为“嵌套数组”的东西。换句话说,一些额外的括号。我正在尝试使用 R 中的 jsonlite 将数据帧转换为 json 数据。下面的可重现代码和结果。谁能指出我如何以正确的格式获取数据(作为嵌套对象的行)?
library(jsonlite)
testdat <- data.frame(locationColumn = c("US", "US"),
nameColumn = c("General Motors", "Walmart"),
zipColumn = c(19890, 72712) )
jsl <- jsonlite::toJSON(
list(
config = list(
item1 = list("country",
"city"),
item2 = "true",
item3 = "false",
item4 = 3
),
rows = split(testdat, 1:nrow(testdat))
),
auto_unbox = TRUE,
pretty = TRUE,
dataframe = "rows",
simplifyDataFrame = TRUE
)
jsl
输出:
{
"config": {
"item1": [
"country",
"city"
],
"item2": "true",
"item3": "false",
"item4": 3
},
"rows": {
"1": [
{
"locationColumn": "US",
"nameColumn": "General Motors",
"zipColumn": 19890
}
],
"2": [
{
"locationColumn": "US",
"nameColumn": "Walmart",
"zipColumn": 72712
}
]
}
}
我需要什么:(编辑:我给 json 增加了一些复杂性。我需要将括号保留在“config”中,但在“rows”中没有括号。
{
"config": {
"item1": [
"country",
"city"
],
"item2": "true",
"item3": "false",
"item4": 3
},
"rows": {
"1":
{
"locationColumn": "US",
"nameColumn": "General Motors",
"zipColumn": 19890
},
"2":
{
"locationColumn": "US",
"nameColumn": "Walmart",
"zipColumn": 72712
}
}
}
【问题讨论】: