亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法將此數據格式轉換為 CSV?

有沒有辦法將此數據格式轉換為 CSV?

ibeautiful 2022-05-24 10:43:28
我有大量帶有格式的提取 Json 文件。我想知道是否有任何方法可以將其轉換為以列作為特征和行中的值的 CSV。{"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}
查看完整描述

2 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

我對您的預期輸出并不完全清楚(請參閱@user5783745 答案的評論和討論)。您的 JSON 字符串包含一些嵌套對象,list如果您使用jsonlite::fromJSON. 由于您沒有為您提供的示例數據提供匹配的預期輸出,因此可能有不同的方法來處理這些嵌套條目。


一種可能性是解析 JSON 字符串,然后在綁定行之前解析兩次flatten結果。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>

結果對象是一個tibble帶有一些list列的對象。要存儲為 CSV,您可以排除這些list列。


樣本數據

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}')



查看完整回答
反對 回復 2022-05-24
?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

您可以輕松地將其轉換為更易于使用 (a list) 的數據格式,但此后如何使用它取決于您自己。在這種情況下,數據列表不會自動變成 a data.frame- 您必須考慮如何轉換它(假設某些列表項是單個項,而其他列表項本身就是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) 


查看完整回答
反對 回復 2022-05-24
  • 2 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號