1

我已经从 yelp 下载了业务 json 文件以在其中进行一些数据挖掘,但该文件是 json 格式,我希望它是 csv 格式。该文件包含以下格式:

{
    'type': 'business',
    'business_id': (encrypted business id),
    'name': (business name),
    'neighborhoods': [(hood names)],
    'full_address': (localized address),
    'city': (city),
    'state': (state),
    'latitude': latitude,
    'longitude': longitude,
    'stars': (star rating, rounded to half-stars),
    'review_count': review count,
    'categories': [(localized category names)]
    'open': True / False (corresponds to closed, not business hours),
    'hours': {
        (day_of_week): {
            'open': (HH:MM),
            'close': (HH:MM)
        },
        ...
    },
    'attributes': {
        (attribute_name): (attribute_value),
        ...
    },
}

如何将其转换为 csv ?

4

2 回答 2

0

你是说CSV吗?JSON 解析起来非常方便,您可以使用 R 轻松将其加载到数据帧中,然后如果仍需要将其保存为 CSV。

这篇文章很好地描述了使用 R 导入 JSON 的方法。完成后,您只需使用 .csv 文件重写 CSV 文件中的数据write.csv()。这是相关的文档:https ://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html

于 2016-04-19T08:59:51.470 回答
0

包:
库(httr)
库(jsonlite)

图书馆 (rlist)

我在将 JSON 转换为数据框/CSV 时遇到问题。对于我的情况,我做了:

Token <- "245432532532"
source <- "http://......."
header_type <- "applcation/json"
full_token <- paste0("Bearer ", Token)
response <- GET(n_source, add_headers(Authorization = full_token, Accept = h_type), timeout(120), verbose())
text_json <- content(response, type = 'text', encoding = "UTF-8")
jfile <- fromJSON( text_json)
df <- as.data.frame(jfile)

然后从 df 到 CSV。如果需要,这种格式应该很容易将其转换为多个 .csv。

重要的部分是内容函数应该有 type = 'text'。

于 2018-03-18T23:46:26.707 回答