gpt4 book ai didi

r - 在 R 中使用 jsonlite 时,如何指定仅将某些条目视为数组?

转载 作者:行者123 更新时间:2023-12-05 04:03:43 26 4
gpt4 key购买 nike

我有以下代码:

# install.packages("jsonlite")
require("jsonlite")
x = list(
test = "my_test",
data = c(1, 2, 3)
)
toJSON(x)

这打印:

{"test":["my_test"],"data":[1,2,3]} 

我期待:

{"test":"my_test","data":[1,2,3]}

我尝试使用 documentation 中的一些参数, 但似乎无法正确处理。

最佳答案

参数 auto_unbox=TRUE 起到了作用:

automatically unbox all atomic vectors of length 1. It is usually safer to avoid this and instead use the unbox function to unbox individual elements. An exception is that objects of class AsIs (i.e. wrapped in I()) are not automatically unboxed. This is a way to mark single values as length-1 arrays.

即,解决方案是 toJSON(x, auto_unbox=TRUE),它返回我预期的结果:

{"test":"my_test","data":[1,2,3]}

关于r - 在 R 中使用 jsonlite 时,如何指定仅将某些条目视为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53425221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com