我正在使用一个预先指定的API定义,我需要遵守:
"myTable": {
"split": [
{
"total": 0,
"perItem": [
0,
0,
0
]
}
]
函数的结果是一个列表(因为我使用了apply):
Export
[[1]]
[[1]]$total
[1] 13
[[1]]$perItem
1 2 3
5 7 0
,但当我将其转换为。json时,它的格式与预定义的API定义不同:
toJSON(Export)
[{"total":[13],"perPlan":[5,7,0]}]
我想知道如何将应用程序的输出转换为预定义的API?
我尝试将此转换为数组:
toJSON(array(Export), simplify = TRUE)
[{"total":[13],"perPlan":[5,7,0]}]
,但这仍然有额外的[]
在总含量。
根据API规范,您的输入也应该"嵌入";将数据放入这个split和mytable列表中,这可以使用:
Export <- list(list(total = 13,
perItem = c(5, 7, 0)))
for_JSON <- list(mytable = list(split = Export))
toJSON(for_JSON, simplify = TRUE, pretty = TRUE)
给了:
{
"mytable": {
"split": [
{
"total": 13,
"perItem": [5, 7, 0]
}
]
}
}
这看起来像API想要什么。