json - 将一个 txt 文件中的多个 JSON 对象转换为 R

标签 json r twitter

我对 Json 文件非常陌生。我抓取了一个包含数百万个 json 对象的 txt 文件,例如:

{
    "created_at":"Mon Oct 14 21:04:25 +0000 2013",
    "default_profile":true,
    "default_profile_image":true,
    "description":"...",
    "followers_count":5,
    "friends_count":560,
    "geo_enabled":true,
    "id":1961287134,
    "lang":"de",
    "name":"Peter Schmitz",
    "profile_background_color":"C0DEED",
    "profile_background_image_url":"http://abs.twimg.com/images/themes", 
    "utc_offset":-28800,
    ...
}
{
    "created_at":"Fri Oct 17 20:04:25 +0000 2015",
    ...
}

我想将列提取到 R 中的数据框中:

Variable          Value
created_at          X     
default_profile     Y     

 …

一般来说,类似于 Python 中的操作( multiple Json objects in one file extract by python )。如果有人有想法或建议,我们将不胜感激!谢谢!

最佳答案

这里是一个关于如何使用两个对象来处理它的示例。我假设您能够从文件中读取 JSON,否则请参阅 here .

myjson = '{"created_at": "Mon Oct 14 21:04:25 +0000 2013", "default_profile": true, 
  "default_profile_image": true, "description": "...", "followers_count": 
    5, "friends_count": 560, "geo_enabled": true, "id": 1961287134, "lang":  
    "de", "name": "Peter Schmitz", "profile_background_color": "C0DEED",  
  "profile_background_image_url": "http://abs.twimg.com/images/themes", "utc_offset": -28800}
{"created_at": "Mon Oct 15 21:04:25 +0000 2013", "default_profile": true, 
  "default_profile_image": true, "description": "...", "followers_count": 
    5, "friends_count": 560, "geo_enabled": true, "id": 1961287134, "lang":  
    "de", "name": "Peter Schmitz", "profile_background_color": "C0DEED",  
  "profile_background_image_url": "http://abs.twimg.com/images/themes", "utc_offset": -28800}
'

library("rjson")

# Split the text into a list of all JSON objects. I chose '!x!x!' pretty randomly.. There may be better ways of keeping the brackets wile splitting.
my_json_objects = head(strsplit(gsub('\\}','\\}!x!x!', myjson),'!x!x!')[[1]],-1)
# read the text as JSON objects 
json_data <- lapply(my_json_objects, function(x) {fromJSON(x)})
# Transform to dataframes
json_data <- lapply(json_data, function(x) {data.frame(val=unlist(x))}) 

输出:

[[1]]
                                                            val
created_at                       Mon Oct 14 21:04:25 +0000 2013
default_profile                                            TRUE
default_profile_image                                      TRUE
description                                                 ...
followers_count                                               5
friends_count                                               560
geo_enabled                                                TRUE
id                                                   1961287134
lang                                                         de
name                                              Peter Schmitz
profile_background_color                                 C0DEED
profile_background_image_url http://abs.twimg.com/images/themes
utc_offset                                               -28800

[[2]]
                                                            val
created_at                       Mon Oct 15 21:04:25 +0000 2013
default_profile                                            TRUE
default_profile_image                                      TRUE
description                                                 ...
followers_count                                               5
friends_count                                               560
geo_enabled                                                TRUE
id                                                   1961287134
lang                                                         de
name                                              Peter Schmitz
profile_background_color                                 C0DEED
profile_background_image_url http://abs.twimg.com/images/themes
utc_offset                                               -28800

希望这有帮助!

关于json - 将一个 txt 文件中的多个 JSON 对象转换为 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48946655/

相关文章:

javascript - 使用 AngularJS.js 获取下拉列表中的 JSON 数据?

jquery - 将json数据获取到li中

r - 为什么 as.character() 在日期列表上返回一个整数?

r - data.table 无法识别过滤器中的逻辑

r - 为列表的每个元素创建一个单独的变量

javascript - 在 user_timeline 上获取 403 Forbidden 但在任何其他 API 上都没有

java - MongoDB Java 驱动程序

javascript - 使用 jquery 在 javascript 中解析\迭代 JSON `bad` 数组。怎么解析这样的东西?

推特光标

ios - 用户时间线的 Twitter JSON URL