首页 > 解决方案 > 从 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)))```

标签: rjsondataframenested

解决方案


推荐阅读