如何将 Json 转换为 R 中的数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36454638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:22:26  来源:igfitidea点击:

How can I convert Json to data frame in R

jsonrdata-analysis

提问by Ping Yi Hsu

I'd like to convert my json data to data frame in R. Here is what I've done so far:

我想将我的 json 数据转换为 R 中的数据框。这是我到目前为止所做的:

library("rjson")
result <- fromJSON(file ="mypath/data.json")
json_data_frame <- as.data.frame(result)

However, it comes to an error like this:

但是,遇到这样的错误:

Error in data.frame(company_id = "12345678", country_name = "China", : arguments imply differing number of rows: 1, 2, 0

data.frame 中的错误(company_id = "12345678", country_name = "China", : 参数意味着不同的行数:1, 2, 0

I also tried the following code:

我还尝试了以下代码:

library("rjson")
result <- fromJSON(file ="mypath/data.json")
final_data <- do.call(rbind, result)

And this error comes up:

这个错误出现了:

Warning message: In (function (..., deparse.level = 1) : number of columns of result is not a multiple of vector length (arg 3)

警告消息:In (function (..., deparse.level = 1) : 结果的列数不是向量长度的倍数 (arg 3)

I don't know what is going on here and how can I solve it. I will appreciate if I could get some help on this.

我不知道这里发生了什么,我该如何解决。如果我能得到一些帮助,我将不胜感激。

Here are some pieces of my json data:

这是我的一些json数据:

{"business_id": "1234567", "Country_name": "China", "hours": {"Monday": {"close": "02:00", "open": "11:00"}, "Tuesday": {"close": "02:00", "open": "11:00"}, "Friday": {"close": "02:00", "open": "11:00"}, "Wednesday": {"close": "02:00", "open": "11:00"}, "Thursday": {"close": "02:00", "open": "11:00"}, "Sunday": {"close": "02:00", "open": "12:00"}, "Saturday": {"close": "02:00", "open": "12:00"}}, "open": true, "categories": ["Bars", "Nightlife", "Restaurants"], "city": "Beijing", "review_count": 5, "name": "Chen's Bar", "neighborhoods": ["West End"], "attributes": {"Take-out": true, "Wi-Fi": "free", "Good For": {"dessert": false, "latenight": false, "lunch": false, "dinner": false, "breakfast": false, "brunch": false}, "Good For Dancing": false, "Noise Level": "loud", "Takes Reservations": false, "Delivery": false, "Ambience": {"romantic": false, "intimate": false, "classy": false, "hipster": false, "divey": false, "touristy": false, "trendy": false, "upscale": false, "casual": false}, "Happy Hour": true, "Parking": {"garage": false, "street": false, "validated": false, "lot": false, "valet": false}, "Has TV": true, "Outdoor Seating": false, "Attire": "casual", "Alcohol": "full_bar", "Waiter Service": true, "Accepts Credit Cards": true, "Good for Kids": false, "Good For Groups": true, "Caters": true, "Price Range": 1}, "type": "business"}

{"business_id": "1234567", "Country_name": "China", "hours": {"Monday": {"close": "02:00", "open": "11:00"}, "Tuesday" ": {"close": "02:00", "open": "11:00"}, "Friday": {"close": "02:00", "open": "11:00"}, "星期三": {"close": "02:00", "open": "11:00"}, "Thursday": {"close": "02:00", "open": "11:00" }, "Sunday": {"close": "02:00", "open": "12:00"}, "Saturday": {"close": "02:00", "open": "12:00" 00"}}, "open": true, "categories": ["酒吧”、“夜生活”、“餐厅”]、“城市”:“北京”、“review_count”:5、“名称”:“陈家酒吧”、“街区”:[“西区”]、“属性”: {“外卖”:真,“Wi-Fi”:“免费”,“适合”:{“甜点”:假,“深夜”:假,“午餐”:假,“晚餐”:假,“早餐”:错误,“早午餐”:错误},“适合跳舞”:错误,“噪音水平”:“大声”,“接受预订”:错误,“交付”:错误,“氛围”:{“浪漫” : false, "intimate": false, "classy": false, "hipster": false, "divey": false, "touristy": false, "时尚”:假,“高档”:假,“休闲”:假},“欢乐时光”:真,“停车”:{“车库”:假,“街道”:假,“验证”:假,“地段” ": false, "valet": false}, "Has TV": true, "Outdoor Seating": false, "Attire": "casual", "Alcohol": "full_bar", "Waiter Service": true, "Accepts Credit Cards": true, "Good for Kids": false, "Good For Groups": true, "Caters": true, "Price Range": 1}, "type": "business"}lot": false, "valet": false}, "Has TV": true, "Outdoor Seating": false, "Attire": "casual", "Alcohol": "full_bar", "Waiter Service": true, "接受信用卡": true, "Good for Kids": false, "Good For Groups": true, "Caters": true, "Price Range": 1}, "type": "business"}lot": false, "valet": false}, "Has TV": true, "Outdoor Seating": false, "Attire": "casual", "Alcohol": "full_bar", "Waiter Service": true, "接受信用卡": true, "Good for Kids": false, "Good For Groups": true, "Caters": true, "Price Range": 1}, "type": "business"}

回答by Kush Patel

Try using jsonlite library. It work for me

尝试使用 jsonlite 库。它对我有用

fromJSON(temp) %>% as.data.frame

Following is output enter image description here

以下是输出 在此处输入图片说明

if you want list.

如果你想要列表。

fromJSON(temp) 

回答by Adarsh Pawar

Load the jsonlite package

加载jsonlite包

library(jsonlite)

wine_json is a JSON

wine_json 是一个 JSON

wine_json <- '{"name":"Chateau Migraine", "year":1997, "alcohol_pct":12.4, "color":"red", "awarded":false}'

Convert wine_json into a list:

将 wine_json 转换为列表:

wine <- fromJSON(wine_json)

Print structure of wine

酒的印刷结构

str(wine)