r - 避免对具有 1 个值的向量进行拆箱
问题描述
对于我希望将数据推送到的 API,我需要避免在特定值上发生拆箱。
考虑以下输入:
library(jsonlite)
lsA <- list(propertyName = "listA",
Values = c("x"))
lsB <- list(propertyName = "listB",
Values = c("a","b","c"))
lsC <- list(propertyName = "listC",
min = 1,
max = 3)
我希望我的输出是这样的:
[
{
"propertyName": "listA",
"Values": ["x"]
},
{
"propertyName": "listB",
"Values": ["a", "b", "c"]
},
{
"propertyName": "listC",
"min": 1,
"max": 3
}
]
但是,当我这样做时:
lsTest <- list()
lsTest <- list.append(lsTest,I(lsA),lsB,lsC)
jsonTest <- jsonlite::toJSON(lsTest,auto_unbox = TRUE, pretty = TRUE)
jsonTest
我得到了这个(注意 listA 的未装箱值):
[
{
"propertyName": "listA",
"Values": "x"
},
{
"propertyName": "listB",
"Values": ["a", "b", "c"]
},
{
"propertyName": "listC",
"min": 1,
"max": 3
}
]
如何避免在 toJSON 转换期间拆箱特定的单元素向量?
编辑: cwthom 好心解决了它。只需更改c("x")
为list("x")
. 它也适用于包含多个项目的列表,并且只添加一些额外的新行,这似乎只是化妆品,对我的最终结果没有任何负面影响。
解决方案
推荐阅读
- python - 如何修复“”TypeError:img应该是PIL Image。得到
我是初学者,我正在学习编写图像分类器。我的目标是创建一个
predict
函数。有什么建议可以解决吗?
在这个项目中,我想使用预测功能来识别不同的花种。所以我可以稍后检查他们的标签。
尝试修复:我已经使用了该
unsqueeze_(0)
方法并从 nu- ssl - ISPConfig LetsEncrypt 和自签名证书 BROKEN CONFIG
- azure-devops - 如何处理 Azure Pipeline 中自定义条件的 Null?
- c# - 如何在不搜索相同字符串的情况下比较两个列表?
- javascript - 如何在 Chart.JS 中从 JSON 和 MySQL 绘制带有时间 x 轴的图表?
- r - 返回带有公式 (~) 波浪号和符号的命名列表的函数
- sql-server - SQL Server 组合行和列
- node.js - Angular 7 HttpClient:ToPromise 不适用于 put 请求
- android - 制作 Instagram 可滑动帖子。意味着拍摄一张图像并将其裁剪成 3 或 4 等份
- react-native - 在本机反应中在单个 onpress 上调用 lib 函数和另一个函数