2 回答

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
我對(duì)您的預(yù)期輸出并不完全清楚(請(qǐng)參閱@user5783745 答案的評(píng)論和討論)。您的 JSON 字符串包含一些嵌套對(duì)象,list如果您使用jsonlite::fromJSON. 由于您沒(méi)有為您提供的示例數(shù)據(jù)提供匹配的預(yù)期輸出,因此可能有不同的方法來(lái)處理這些嵌套條目。
一種可能性是解析 JSON 字符串,然后在綁定行之前解析兩次flatten結(jié)果。list
library(tidyverse)
library(jsonlite)
map(json, ~fromJSON(.x) %>% flatten() %>% flatten()) %>% bind_rows()
## A tibble: 2 x 15
# state text has_emoji created_at id indices screen_name name id_str
# <chr> <chr> <lgl> <chr> <dbl> <list> <chr> <chr> <chr>
#1 New … WeTh… FALSE Mon Sep 0… 2.75e7 <int [… joncoopert… Jon … 27493…
#2 Indi… "RT … FALSE Mon Sep 0… 1.68e9 <int [… dariusherr… Dari… 16808…
## … with 6 more variables: source <chr>, location <chr>, verified <lgl>,
## url <chr>, expanded_url <chr>, display_url <chr>
結(jié)果對(duì)象是一個(gè)tibble帶有一些list列的對(duì)象。要存儲(chǔ)為 CSV,您可以排除這些list列。
樣本數(shù)據(jù)
json <- c(
'{"state": "New Jersey", "text": "RT @joncoopertweets: Register to join the #WeThePeopleMarch on September 21st in Washington, D.C. \u2014 or one of the 50+ marches that will be\u2026", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246349467649, "entities": {"hashtags": [{"text": "WeThePeopleMarch", "indices": [42, 59]}], "urls": [], "user_mentions": [{"screen_name": "joncoopertweets", "name": "Jon Cooper", "id": 27493883, "id_str": "27493883", "indices": [3, 19]}], "symbols": []}, "source": "Twitter for iPad", "location": "Leonia, NJ", "verified": false, "geocode": null}',
'{"state": "Indiana", "text": "RT @dariusherron1: Don\u2019t nobody love they girl like Mexicans ", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246378827776, "entities": {"hashtags": [], "urls": [{"url": "", "expanded_url": "", "display_url": "", "indices": [61, 84]}], "user_mentions": [{"screen_name": "dariusherron1", "name": "Darius Herron", "id": 1680891876, "id_str": "1680891876", "indices": [3, 17]}], "symbols": []}, "source": "Twitter for iPhone", "location": "Indianapolis, IN", "verified": false, "geocode": null}')

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以輕松地將其轉(zhuǎn)換為更易于使用 (a list) 的數(shù)據(jù)格式,但此后如何使用它取決于您自己。在這種情況下,數(shù)據(jù)列表不會(huì)自動(dòng)變成 a data.frame- 您必須考慮如何轉(zhuǎn)換它(假設(shè)某些列表項(xiàng)是單個(gè)項(xiàng),而其他列表項(xiàng)本身就是data.frames
a <- '{"state": "New Jersey", "text": "RT @joncoopertweets: Register to join the #WeThePeopleMarch on September 21st in Washington, D.C. \u2014 or one of the 50+ marches that will be\u2026", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246349467649, "entities": {"hashtags": [{"text": "WeThePeopleMarch", "indices": [42, 59]}], "urls": [], "user_mentions": [{"screen_name": "joncoopertweets", "name": "Jon Cooper", "id": 27493883, "id_str": "27493883", "indices": [3, 19]}], "symbols": []}, "source": "Twitter for iPad", "location": "Leonia, NJ", "verified": false, "geocode": null}'
library(jsonlite)
library(dplyr)
a <- a %>% fromJSON
new_dataframe <- data.frame(state=character(),
text=character(),
has_emoji=character(),
id=character(),
entities=character(), stringsAsFactors = FALSE)
new_dataframe[1, ] <- c(a$state, a$text, a$has_emoji, a$created_at, a$id)
添加回答
舉報(bào)