【发布时间】:2023-03-18 07:12:01
【问题描述】:
我有一个 R 数据框,我想将其转换为 JSON,但我想将每一行插入到名为“traits”的新 JSON 对象中。我尝试将特征列作为第一列并将新数据框转换为 JSON,但这不会产生正确的输出。我还尝试将“特征”:{ 对象附加到每个转换的 JSON 输出,但这也失败了。我正在尝试在数据框内工作,然后将其转换为 JSON,因为 toJSON 会在括号 [] 中生成一个列表,我也无法绕过它。我正在使用 jsonlite
color = c('red','blue','green')
fruit = c('apple','orange','grape')
animal = c('cat','dog','chicken')
df<- data.frame(color, fruit, animal)
toJSON(df, pretty= TRUE)
我希望它看起来像这样:
[
{ "traits": {
"color": "red",
"fruit": "apple",
"animal": "cat"
}
},
【问题讨论】: