正在将 JSON 文件加载到 R

Loading JSON file into R

我想将这个 JSON 文件(twitter 数据)插入到 R 中,并想制作一个这样的列表

但我得到了这样的东西

我的 JSON 看起来像这样(这只是一个例子)

[{"contributors": null, "truncated": false, "text": "RT @KazmiWajahat: Indian media including @CNNnews18 confirming Pakistan's retaliation at LoC forward areas with heavy firing and shelling w\u2026", "is_quote_status": false}]

您可以使用jsonlite::fromJSON解析JSON文件

示例基于您的 JSON 示例字符串

ss <- '[{"contributors": null, "truncated": false, "text": "RT @KazmiWajahat: Indian media including @CNNnews18 confirming Pakistans retaliation at LoC forward areas with heavy firing and shelling w\u2026", "is_quote_status": false}]'

library(jsonlite)
fromJSON(ss)
#  contributors truncated
#1           NA     FALSE
#                                                                                                                                       text
#1 RT @KazmiWajahat: Indian media including @CNNnews18 confirming Pakistans retaliation at LoC forward areas with heavy firing and shelling w…
#  is_quote_status
#1           FALSE

由于您提供的样本数据最少,您最终得到的 data.frame 只有一行。

jsonlite vignette

中举一个稍微复杂一点的例子
ss <-'[
    {"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"}, 
    {"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"},
    {"Name" : "Bowser", "Occupation" : "Koopa"}]'

您可以看到 fromJSON 如何解析 JSON 字符串和 returns 一个 data.frame

fromJSON(ss)
#    Name Age Occupation
#1  Mario  32    Plumber
#2  Peach  21   Princess
#3 Bowser  NA      Koopa