我正在努力与我认为应该是一个简单的问题,并且它变得令人沮丧。我在R中有一个列表对象,其中一些列表项是JSON对象。特别是,我有以下变量:
> dput(node)
structure(list(teamname = "", round = 5, id = 31, seeding = NA_real_,
wins = 0, losses = 0, completed = FALSE, r1odds = structure("{\"Rockets\":0.2,\"Timberwolves\":0.01,\"Jazz\":0.04,\"Thunder\":0.03,\"Warriors\":0.25,\"Spurs\":0.02,\"Trail Blazers\":0.07,\"Pelicans\":0.02,\"Raptors\":0.05,\"Wizards\":0.01,\"Cavaliers\":0.12,\"Pacers\":0.04,\"Celtics\":0.04,\"Bucks\":0.02,\"76ers\":0.06,\"Heat\":0.02}", class = "json"),
r2odds = structure("{\"Rockets\":0.25,\"Jazz\":0.05,\"Warriors\":0.35,\"Pelicans\":0.07,\"Raptors\":0.06,\"Cavaliers\":0.11,\"Celtics\":0.05,\"76ers\":0.06}", class = "json"),
r3odds = structure("{\"Rockets\":0.35,\"Warriors\":0.41,\"Cavaliers\":0.15,\"Celtics\":0.09}", class = "json")), .Names = c("teamname",
"round", "id", "seeding", "wins", "losses", "completed", "r1odds",
"r2odds", "r3odds"))
> node
$teamname
[1] ""
$round
[1] 5
$id
[1] 31
$seeding
[1] NA
$wins
[1] 0
$losses
[1] 0
$completed
[1] FALSE
$r1odds
{"Rockets":0.2,"Timberwolves":0.01,"Jazz":0.04,"Thunder":0.03,"Warriors":0.25,"Spurs":0.02,"Trail Blazers":0.07,"Pelicans":0.02,"Raptors":0.05,"Wizards":0.01,"Cavaliers":0.12,"Pacers":0.04,"Celtics":0.04,"Bucks":0.02,"76ers":0.06,"Heat":0.02}
$r2odds
{"Rockets":0.25,"Jazz":0.05,"Warriors":0.35,"Pelicans":0.07,"Raptors":0.06,"Cavaliers":0.11,"Celtics":0.05,"76ers":0.06}
$r3odds
{"Rockets":0.35,"Warriors":0.41,"Cavaliers":0.15,"Celtics":0.09}
> class(node$r2odds)
[1] "json"
请注意元素$ r3odds,$ r2odds等在这里属于JSON类。但是,当我运行以下内容时:
jsonlite::toJSON(node, pretty = TRUE, auto_unbox = TRUE)
由于某种原因,它将每个对象值包装在引号中:
{
"teamname": "",
"round": 5,
"id": 31,
"seeding": "NA",
"wins": 0,
"losses": 0,
"completed": false,
"r1odds": "{\"Rockets\":0.2,\"Timberwolves\":0.01,\"Jazz\":0.04,\"Thunder\":0.03,\"Warriors\":0.25,\"Spurs\":0.02,\"Trail Blazers\":0.07,\"Pelicans\":0.02,\"Raptors\":0.05,\"Wizards\":0.01,\"Cavaliers\":0.12,\"Pacers\":0.04,\"Celtics\":0.04,\"Bucks\":0.02,\"76ers\":0.06,\"Heat\":0.02}",
"r2odds": "{\"Rockets\":0.25,\"Jazz\":0.05,\"Warriors\":0.35,\"Pelicans\":0.07,\"Raptors\":0.06,\"Cavaliers\":0.11,\"Celtics\":0.05,\"76ers\":0.06}",
"r3odds": "{\"Rockets\":0.35,\"Warriors\":0.41,\"Cavaliers\":0.15,\"Celtics\":0.09}"
}
虽然我希望返回的对象看起来像这样:
{
"teamname": "",
"round": 5,
"id": 31,
"seeding": "NA",
"wins": 0,
"losses": 0,
"completed": false,
"r1odds":{\"Rockets\":0.2,\"Timberwolves\":0.01,\"Jazz\":0.04,\"Thunder\":0.03,\"Warriors\":0.25,\"Spurs\":0.02,\"Trail Blazers\":0.07,\"Pelicans\":0.02,\"Raptors\":0.05,\"Wizards\":0.01,\"Cavaliers\":0.12,\"Pacers\":0.04,\"Celtics\":0.04,\"Bucks\":0.02,\"76ers\":0.06,\"Heat\":0.02},
"r2odds":{\"Rockets\":0.25,\"Jazz\":0.05,\"Warriors\":0.35,\"Pelicans\":0.07,\"Raptors\":0.06,\"Cavaliers\":0.11,\"Celtics\":0.05,\"76ers\":0.06},
"r3odds":{\"Rockets\":0.35,\"Warriors\":0.41,\"Cavaliers\":0.15,\"Celtics\":0.09}
}
并且最好不要在对象键周围包含所有转义字符。这有可能吗?
谢谢!
答案 0 :(得分:2)
您有一些包含JSON
项和非JSON
项的列表,toJSON
不喜欢这样。首先我们回到标准列表结构,然后我们使用toJSON
:
library(jsonlite)
library(purrr)
node %>%
modify_at(8:10,fromJSON) %>%
toJSON(pretty=TRUE, auto_unbox = TRUE)
# {
# "teamname": "",
# "round": 5,
# "id": 31,
# "seeding": "NA",
# "wins": 0,
# "losses": 0,
# "completed": false,
# "r1odds": {
# "Rockets": 0.2,
# "Timberwolves": 0.01,
# "Jazz": 0.04,
# "Thunder": 0.03,
# "Warriors": 0.25,
# "Spurs": 0.02,
# "Trail Blazers": 0.07,
# "Pelicans": 0.02,
# "Raptors": 0.05,
# "Wizards": 0.01,
# "Cavaliers": 0.12,
# "Pacers": 0.04,
# "Celtics": 0.04,
# "Bucks": 0.02,
# "76ers": 0.06,
# "Heat": 0.02
# },
# "r2odds": {
# "Rockets": 0.25,
# "Jazz": 0.05,
# "Warriors": 0.35,
# "Pelicans": 0.07,
# "Raptors": 0.06,
# "Cavaliers": 0.11,
# "Celtics": 0.05,
# "76ers": 0.06
# },
# "r3odds": {
# "Rockets": 0.35,
# "Warriors": 0.41,
# "Cavaliers": 0.15,
# "Celtics": 0.09
# }
# }