创建字符串 - 从方括号中删除外部引号
Create string - Remove outer quotes from squared brackets
我需要创建以下字符串作为负载:
{"url":"http://google.com","number":["123"]}
当基于列表 (payload_2) 创建字符串时,我得到以下结果:
{"url":"http://google.com","number":"[\"123\"]"}
问题:
如何删除方括号 (payload_2) 中的外引号?
尝试解决问题:
# --------------------------------
# Payload from string (this works).
# --------------------------------
payload_1 <- "{\"url\":\"http://google.com\",\"number\":[\"123\"]}"
# --------------------
# Payload from list (does not work, quotations surrounding squared brackets, still there).
# --------------------
payload_2 <- list(
"url" = "http://google.com",
"number" = "[123]"
)
payload_2 <- toJSON(payload_2, auto_unbox = TRUE)
cat(paste0("payload from string: ", payload_1, "\n"))
cat(paste0("payload from list: ", payload_2))
# -----------------------------------------
# Test 1 - Using [noquote] - throws errors.
# -----------------------------------------
number <- "[\"123\"]"
number <- noquote(number)
payload_3 <- list(
"url" = "http://google.com",
"number" = number
)
payload_3 <- toJSON(payload_3, auto_unbox = TRUE)
# Error: No method asJSON S3 class: noquote
结果
来自字符串的负载:{"url":"http://google.com","number":["123"]}
来自列表的负载:{"url":"http://google.com","number":"[\"123\"]"}
Payload_3: 错误: 没有方法作为 JSON S3 class: noquote
当您想在 R 中的字符串中使用双引号时,请不要使用转义符,而是使用单引号将字符串括起来:
# These two are equivalent
number <- "[\"123\"]"
number <- '["123"]'
此外,当您包含包中的函数时,请告诉我们您使用的是什么包,这样我们就不必猜测了。我猜你正在使用 jsonlite::toJSON
。你没有得到你想要的结果的原因是因为 jsonlite::toJSON
是为了给你有效的 JSON 代码而你想要的输出不是。如果你真的想要那个,那么你必须在之后使用字符串操作手动调整它。
payload_2 <- list(
"url" = "http://google.com",
"number" = "[123]"
)
payload_2 <- jsonlite::toJSON(payload_2, auto_unbox = TRUE)
payload_2a <- stringr::str_replace(payload_2, '\"\[', '["')
payload_2a <- stringr::str_replace(payload_2a, '\]\"', '"]')
我需要创建以下字符串作为负载:
{"url":"http://google.com","number":["123"]}
当基于列表 (payload_2) 创建字符串时,我得到以下结果:
{"url":"http://google.com","number":"[\"123\"]"}
问题:
如何删除方括号 (payload_2) 中的外引号?
尝试解决问题:
# --------------------------------
# Payload from string (this works).
# --------------------------------
payload_1 <- "{\"url\":\"http://google.com\",\"number\":[\"123\"]}"
# --------------------
# Payload from list (does not work, quotations surrounding squared brackets, still there).
# --------------------
payload_2 <- list(
"url" = "http://google.com",
"number" = "[123]"
)
payload_2 <- toJSON(payload_2, auto_unbox = TRUE)
cat(paste0("payload from string: ", payload_1, "\n"))
cat(paste0("payload from list: ", payload_2))
# -----------------------------------------
# Test 1 - Using [noquote] - throws errors.
# -----------------------------------------
number <- "[\"123\"]"
number <- noquote(number)
payload_3 <- list(
"url" = "http://google.com",
"number" = number
)
payload_3 <- toJSON(payload_3, auto_unbox = TRUE)
# Error: No method asJSON S3 class: noquote
结果
来自字符串的负载:{"url":"http://google.com","number":["123"]}
来自列表的负载:{"url":"http://google.com","number":"[\"123\"]"}
Payload_3: 错误: 没有方法作为 JSON S3 class: noquote
当您想在 R 中的字符串中使用双引号时,请不要使用转义符,而是使用单引号将字符串括起来:
# These two are equivalent
number <- "[\"123\"]"
number <- '["123"]'
此外,当您包含包中的函数时,请告诉我们您使用的是什么包,这样我们就不必猜测了。我猜你正在使用 jsonlite::toJSON
。你没有得到你想要的结果的原因是因为 jsonlite::toJSON
是为了给你有效的 JSON 代码而你想要的输出不是。如果你真的想要那个,那么你必须在之后使用字符串操作手动调整它。
payload_2 <- list(
"url" = "http://google.com",
"number" = "[123]"
)
payload_2 <- jsonlite::toJSON(payload_2, auto_unbox = TRUE)
payload_2a <- stringr::str_replace(payload_2, '\"\[', '["')
payload_2a <- stringr::str_replace(payload_2a, '\]\"', '"]')