r 转换为 json,需要将值 null 取消引用

r converted to json, need value null to be unquoted

我正在发送一个 http 请求,正文需要采用 json 格式。作为标准,似乎引用了 null 值。这会阻止 http 请求正常工作。当测试手动构建字符串并删除值 null 的引号时,http 请求工作正常。

问题:

jsonlite 能否去除所有空值的引号?

我当前的代码:

library(jsonlite)

x <- list(epic = "Stockholm", currency = "null")
json <- toJSON(x,  auto_unbox = TRUE)

给出结果,但不起作用:

{"epic":"Stockholm","currency":"null"} 

这个手动构建的字符串有效:

{"epic":"Stockholm","currency": null} 

正如所见 in the docs,您可能需要使用 NA 而不是 "null" :

library(jsonlite)

x <- list(epic = "Stockholm", currency = NA)
json <- toJSON(x,  auto_unbox = TRUE, na = "null")

以下有效并解决了问题。

我的源数据似乎需要指定值 NULL 而不是 "null",以及如何对值 null 进行编码的设置。

library(jsonlite)

x <- list(epic = "Stockholm", currency = NULL)
json <- toJSON(x,  auto_unbox = TRUE, null = "null")