r - 从 r 中的嵌套数据框构建 json
问题描述
我正在努力构建一个json。
我有一个包含 3 行和两列的数据框:“id”(id 列表),然后是“text”(推文)。
df$id= c(78198310004451, 78198310004451, 88198310004453)
df$text = c("I love you", "I just got married!", "I just got a new job!")
and four other fixed variables whose values are static:
Models = c(1:7)
orgId= 1
and two attributes of the twitter id
include_outcome: logi FALSE
twitterId = 70051429
我拉下模板 json 并将其转换为数据框(见下文),以 3 条推文为例。
我无法弄清楚如何从我上面提到的预先存在的数据框(然后我将其转换为 json (toJSON) )生成这样的数据框。
List of 3
$ Models: num [1:7] 1 2 3 4 5 6 7
$ orgId : num 1
$ userData :List of 1
..$ :List of 3
.. ..$ tweets :List of 3
.. .. ..$ :List of 2
.. .. .. ..$ text: chr "I love you"
.. .. .. ..$ id : num 78198310004451
.. .. ..$ :List of 2
.. .. .. ..$ text: chr "I just got married!"
.. .. .. ..$ id : num 78198310004452
.. .. ..$ :List of 2
.. .. .. ..$ text: chr "I just got a new job!"
.. .. .. ..$ id : num 88198310004453
.. ..$ twitterId : num 70051429
.. ..$ include_outcome: logi FALSE
这是输入输出
list(Models = c(1, 2, 3, 4, 5, 6, 7), orgId = 1, userData = list(
list(tweets = list(list(text = "I love you", id = 78198310004451),
list(text = "I just got married!", id = 78198310004452),
list(text = "I just got a new job!", id = 88198310004453)),
twitterId = 70051429, include_outcome = FALSE)))```
解决方案
推荐阅读
- python - 我已通过 pyttx3 将此代码用于文本到语音,并收到此错误,我不知道如何解决
- c - 二维数组中缺少最后一个元素
- r - 查找对象在 Rstudio 中具有最大值的时间
- git - 如何让 git diff 显示所有内容而无需按 Enter 向下滚动?
- python - 如何使用行号作为 bin 绘制直方图?
- android - 为什么我无法让 Google 的“从活动中获取结果”工作?
- python - VS Code 无法识别 Python 虚拟环境包 - 它显示红色下划线
- django - Django 实时通知
- git - 使用 git,如何在不恢复移动的情况下将文本更改恢复到文件?
- jquery - 如何在方法外访问jquery更改事件值?