r - 用 jsonlite 在 R 中构造 json - 括号太多
问题描述
不是 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": {
"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
}
}
}
解决方案
这是一个可能的解决方案:
library(jsonlite)
testdat <- data.frame(locationColumn = c("US", "US"),
nameColumn = c("General Motors", "Walmart"),
zipColumn = c(19890, 72712) )
jsl <- jsonlite::toJSON(
list(
rows = split(testdat, 1:nrow(testdat))
),
auto_unbox = TRUE,
pretty = TRUE,
dataframe = "columns", #change from rows (moves brackets from row level to value level)
simplifyDataFrame = TRUE
)
#removed the backets if desired
#jsl<-gsub("\\[|\\]", "", jsl)
all.equal(testcase, fromJSON(jsl))
testcase<-fromJSON('{
"rows": {
"1":{
"locationColumn": "US",
"nameColumn": "General Motors",
"zipColumn": 19890
},
"2":{
"locationColumn": "US",
"nameColumn": "Walmart",
"zipColumn": 72712
}
}
}')
all.equal(testcase, fromJSON(jsl))
#[1] TRUE
编辑这是一个批准的版本,它手动编辑列表列表以获得正确的格式。
#create a list of the data
top<-list(
config = list(
item1 = list("country",
"city"),
item2 = "true",
item3 = "false",
item4 = 3
),
rows = split(testdat, 1:nrow(testdat))
)
#edit the data frames store as part of rows
#lapply - lapply loops will parse each column in each row to create a new list
rows<-lapply(top$rows, function(x){
tempdf<-x
#collist<-lapply(names(tempdf), function(y){print(tempdf[ , y, drop=T])})
collist<-lapply(names(tempdf), function(y){tempdf[, y, drop=T]})
names(collist)<-names(tempdf)
collist
})
#update the list with the list of list
top$rows<-rows
#make the JSON
jsl <- jsonlite::toJSON(
top,
auto_unbox = TRUE,
pretty = TRUE,
dataframe = "columns",
simplifyDataFrame = TRUE
)
推荐阅读
- reactjs - 反应数组的映射对象键以显示为表头失败
- javascript - 不使用背景图像的视差效果?
- javascript - 无法正确控制 state 和 ref hook 的值
- c - 在连接到最大节点数的有向图中找到节点的最佳方法是什么?
- c - In POSIX, why can't a single condvar be used with multiple mutexes?
- android - Focus input in React webpage is not popping android keypad
- c++ - 被释放后修改 - 在结构中返回一个 C 数组 `c_str` (const char*)
- pandas - 当我不希望它时,PANDAS 将变量转换为年频率
- swift - Xcode 12.2 中 XCFramework 类型库的集成错误
- mysql - 启动“XAMPP”堆栈时出错:无法计算 MAC 地址:dyld:惰性符号绑定失败:未找到符号:_clock_gettime