R将jsonlite::unbox命令应用于列表
我想与需要特定格式的API通信,请参见以下内容:R将jsonlite::unbox命令应用于列表,r,list,jsonlite,unbox,R,List,Jsonlite,Unbox,我想与需要特定格式的API通信,请参见以下内容: library(jsonlite) list(limits = list("Overall_Wave3/0" = unbox("14000"), "Overall_Wave3/1" = unbox("14005"))) 其中给出(注意,该列表的索引为[x]): 现在在我的实际使用案例中,我需要在一个列表中创建数百个这样的元素,所
library(jsonlite)
list(limits = list("Overall_Wave3/0" = unbox("14000"),
"Overall_Wave3/1" = unbox("14005")))
其中给出(注意,该列表的索引为[x]):
现在在我的实际使用案例中,我需要在一个列表中创建数百个这样的元素,所以我需要以某种方式实现自动化。我的输入将是一个数据帧(或TIBLE),我需要将其转换为该格式。但是,只有在未成功执行取消装箱操作的情况下,我才能使其正常工作。也就是说,我已经走了多远:
library(tidyverse)
library(jsonlite)
dat <- data.frame(marker = c("Overall_Wave3/0", "Overall_Wave3/0"),
value = c(14000, 14005)) %>%
mutate(value = as.character(value))
args <- as.list(dat$value)
names(args) <- dat$marker
list(limits = args)
现在,使用toJSON(…)
将这两个列表转换为JSON正文会得到不同的结果:
- 第一个命令给出:
{“limits”:{“totall_Wave3/0”:“14000”,“totall_Wave3/0.1”:“14005”}
- 第二个命令给出:
{“limits”:{“totall_Wave3/0”:[“14000”],“totall_Wave3/0.1”:[“14005”]}
谢谢。关于
toJSON()
中的auto\u unbox=T
参数如何?很好,这很好。
library(tidyverse)
library(jsonlite)
dat <- data.frame(marker = c("Overall_Wave3/0", "Overall_Wave3/0"),
value = c(14000, 14005)) %>%
mutate(value = as.character(value))
args <- as.list(dat$value)
names(args) <- dat$marker
list(limits = args)
$limits
$limits$`Overall_Wave3/0`
[1] "14000"
$limits$`Overall_Wave3/0`
[1] "14005"