我如何在R中将Json转换为 Dataframe

wqlqzqxt  于 2023-01-27  发布在  其他
关注(0)|答案(2)|浏览(135)

我想把json数据转换成R中的 Dataframe 。下面是我目前所做的工作:

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

但是,它会出现这样的错误:
数据框架错误(公司ID ="12345678",国家/地区名称="中国",:参数表示不同的行数:一、二、零
我还尝试了以下代码:

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

出现了这个错误:
警告信息:In(函数(...,偏离水平= 1):结果的列数不是向量长度的倍数(arg 3)
我不知道这是怎么回事,我该如何解决它。如果我能得到一些帮助,我将不胜感激。
下面是我的一些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"}
ohtdti5x

ohtdti5x1#

尝试使用jsonlite库。它适合我

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

以下是输出

如果你想要名单话

fromJSON(temp)
qxsslcnc

qxsslcnc2#

加载jsonlite包

library(jsonlite)

wine_json是一个JSON

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

将wine_json转换为列表:

wine <- fromJSON(wine_json)

打印葡萄酒结构

str(wine)

相关问题