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

How can I convert Json to data frame in R

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

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

然而,却出现了这样的错误:

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

我也试过下面的代码:

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

出现这个错误:

Warning message: In (function (..., deparse.level = 1) : number of columns of result is not a multiple of vector length (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":真,"Wi-Fi":"free","Good For":{"dessert":假,"latenight":假,"lunch":假,"dinner":假,"breakfast":假,"brunch":假},"Good For Dancing":假,"Noise Level":"loud","Takes Reservations":假, "Delivery": 假, "Ambience": {"romantic": 假, "intimate": 假, "classy": 假, "hipster": 假, "divey":假,"touristy":假,"trendy":假,"upscale":假,"casual":假},"Happy Hour":真,"Parking": {"garage": 假, "street": 假, "validated": 假, "lot": 假, "valet": 假}, "Has TV" :正确,"Outdoor Seating":错误,"Attire":"casual","Alcohol":"full_bar","Waiter Service":正确,"Accepts Credit Cards" : 正确, "Good for Kids": 错误, "Good For Groups": 正确, "Caters": 正确, "Price Range": 1}, "type": "business"}

尝试使用 jsonlite 库。对我有用

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

输出如下

如果你想要列表。

fromJSON(temp) 

加载 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)

wine 的打印结构

str(wine)