0

我正在尝试读取 Yelp API 返回的大型 JSON 文件(存储为 .txt 文件),并将其转换为数据框。我的 JSON 文件采用“漂亮打印”格式,前 3 个 JSON 对象如下:

{
    "businesses": [
        {
            "address1": "11301 Wilshire Blvd", 
            "address2": "", 
            "address3": "", 
            "avg_rating": 3.0, 
            "categories": [
                {
                    "category_filter": "hospitals", 
                    "name": "Hospitals", 
                    "search_url": "http://www.yelp.com/search?cflt=hospitals&find_desc=&find_loc=11301+Wilshire+Blvd%2C+Los+Angeles+90073"
                }
            ], 
            "city": "Los Angeles", 
            "country": "USA", 
            "country_code": "US", 
            "distance": 0.0, 
            "id": "9yWDlJ5l1i6O36Fxp5JIBw", 
            "is_closed": false, 
            "mobile_url": "http://m.yelp.com/biz/west-los-angeles-medical-center-los-angeles-2", 
            "name": "West Los Angeles Medical Center", 
            "nearby_url": "http://www.yelp.com/search?find_desc=&find_loc=11301+Wilshire+Blvd%2C+Los+Angeles+90073", 
            "neighborhoods": [], 
            "phone": "3104783711", 
            "photo_url": "http://media2.fl.yelpcdn.com/bpthumb/IZ82DgJAy8emp4dX7UvbUw/ms", 
            "photo_url_small": "http://media2.fl.yelpcdn.com/bpthumb/IZ82DgJAy8emp4dX7UvbUw/ss", 
            "rating_img_url": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/34bc8086841c/ico/stars/v1/stars_3.png", 
            "rating_img_url_small": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/902abeed0983/ico/stars/v1/stars_small_3.png", 
            "review_count": 40, 
            "reviews": [
                {
                    "date": "2014-11-25", 
                    "id": "wO4jShjiPoWDBR_OV3cGmQ", 
                    "mobile_uri": "/biz/west-los-angeles-medical-center-los-angeles-2?full=True&hrid=wO4jShjiPoWDBR_OV3cGmQ", 
                    "rating": 5, 
                    "rating_img_url": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png", 
                    "rating_img_url_small": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png", 
                    "text_excerpt": "I dropped my super expensive insurance, that is not feasible to afford now, and joined the VA.  Shortly after signing up it was found that I needed surgery....", 
                    "url": "http://www.yelp.com/biz/west-los-angeles-medical-center-los-angeles-2?hrid=wO4jShjiPoWDBR_OV3cGmQ", 
                    "user_name": "Dj D.", 
                    "user_photo_url": "http://media1.fl.yelpcdn.com/upthumb/s1RKYlrzhKCZs_zSS0cVOA/ms", 
                    "user_photo_url_small": "http://media1.fl.yelpcdn.com/upthumb/s1RKYlrzhKCZs_zSS0cVOA/ss", 
                    "user_url": "http://www.yelp.com/user_details?userid=cJyDfLw9uJT63MwFgz7XnA"
                }
            ], 
            "state": "CA", 
            "state_code": "CA", 
            "url": "http://www.yelp.com/biz/west-los-angeles-medical-center-los-angeles-2", 
            "zip": "90073"
        }, 
        {
            "address1": "11301 Wilshire", 
            "address2": "Bldg 306", 
            "address3": "", 
            "avg_rating": 3.0, 
            "categories": [
                {
                    "category_filter": "cafeteria", 
                    "name": "Cafeteria", 
                    "search_url": "http://www.yelp.com/search?cflt=cafeteria&find_desc=&find_loc=11301+Wilshire%2C+Los+Angeles+90073"
                }
            ], 
            "city": "Los Angeles", 
            "country": "USA", 
            "country_code": "US", 
            "distance": 0.0, 
            "id": "K8eEx2J3pF3b-w6EZwKY5w", 
            "is_closed": false, 
            "mobile_url": "http://m.yelp.com/biz/va-canteen-wla-los-angeles", 
            "name": "VA Canteen WLA", 
            "nearby_url": "http://www.yelp.com/search?find_desc=&find_loc=11301+Wilshire%2C+Los+Angeles+90073", 
            "neighborhoods": [], 
            "phone": "3104783711", 
            "photo_url": "http://s3-media2.fl.yelpcdn.com/assets/srv0/yelp_styleguide/5f69f303f17c/assets/img/default_avatars/business_medium_square.png", 
            "photo_url_small": "http://s3-media3.fl.yelpcdn.com/assets/srv0/yelp_styleguide/6671667140ef/assets/img/default_avatars/business_small_square.png", 
            "rating_img_url": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/34bc8086841c/ico/stars/v1/stars_3.png", 
            "rating_img_url_small": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/902abeed0983/ico/stars/v1/stars_small_3.png", 
            "review_count": 4, 
            "reviews": [
                {
                    "date": "2014-11-02", 
                    "id": "rzoQx7o9sla7ig3QZAjtUg", 
                    "mobile_uri": "/biz/va-canteen-wla-los-angeles?full=True&hrid=rzoQx7o9sla7ig3QZAjtUg", 
                    "rating": 3, 
                    "rating_img_url": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/34bc8086841c/ico/stars/v1/stars_3.png", 
                    "rating_img_url_small": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/902abeed0983/ico/stars/v1/stars_small_3.png", 
                    "text_excerpt": "This place serves its function. There are a few stations where you can grab food if you don't want to venture off the VA premises for lunch. However, the...", 
                    "url": "http://www.yelp.com/biz/va-canteen-wla-los-angeles?hrid=rzoQx7o9sla7ig3QZAjtUg", 
                    "user_name": "James W.", 
                    "user_photo_url": "http://media1.fl.yelpcdn.com/upthumb/6UlTMXf0VkFmmmXwXe8Flg/ms", 
                    "user_photo_url_small": "http://media1.fl.yelpcdn.com/upthumb/6UlTMXf0VkFmmmXwXe8Flg/ss", 
                    "user_url": "http://www.yelp.com/user_details?userid=qgXcgfdrk5tzmLBq4_h6mQ"
                }
            ], 
            "state": "CA", 
            "state_code": "CA", 
            "url": "http://www.yelp.com/biz/va-canteen-wla-los-angeles", 
            "zip": "90073"
        }, 
        {
            "address1": "11301 Wilshire", 
            "address2": "Bldg 306", 
            "address3": "", 
            "avg_rating": 2.0, 
            "categories": [
                {
                    "category_filter": "cafeteria", 
                    "name": "Cafeteria", 
                    "search_url": "http://www.yelp.com/search?cflt=cafeteria&find_desc=&find_loc=11301+Wilshire%2C+Los+Angeles+90073"
                }
            ], 
            "city": "Los Angeles", 
            "country": "USA", 
            "country_code": "US", 
            "distance": 0.0, 
            "id": "4etl04G_-VwP8NJ2F3nu4w", 
            "is_closed": false, 
            "mobile_url": "http://m.yelp.com/biz/va-canteen-wla-2-los-angeles", 
            "name": "VA Canteen WLA 2", 
            "nearby_url": "http://www.yelp.com/search?find_desc=&find_loc=11301+Wilshire%2C+Los+Angeles+90073", 
            "neighborhoods": [], 
            "phone": "3104783711", 
            "photo_url": "http://s3-media2.fl.yelpcdn.com/assets/srv0/yelp_styleguide/5f69f303f17c/assets/img/default_avatars/business_medium_square.png", 
            "photo_url_small": "http://s3-media3.fl.yelpcdn.com/assets/srv0/yelp_styleguide/6671667140ef/assets/img/default_avatars/business_small_square.png", 
            "rating_img_url": "http://s3-media2.fl.yelpcdn.com/assets/2/www/img/b561c24f8341/ico/stars/v1/stars_2.png", 
            "rating_img_url_small": "http://s3-media2.fl.yelpcdn.com/assets/2/www/img/a6210baec261/ico/stars/v1/stars_small_2.png", 
            "review_count": 1, 
            "reviews": [
                {
                    "date": "2014-02-01", 
                    "id": "G9Qr5OpQHs0qo89LFzYIGA", 
                    "mobile_uri": "/biz/va-canteen-wla-2-los-angeles?full=True&hrid=G9Qr5OpQHs0qo89LFzYIGA", 
                    "rating": 2, 
                    "rating_img_url": "http://s3-media2.fl.yelpcdn.com/assets/2/www/img/b561c24f8341/ico/stars/v1/stars_2.png", 
                    "rating_img_url_small": "http://s3-media2.fl.yelpcdn.com/assets/2/www/img/a6210baec261/ico/stars/v1/stars_small_2.png", 
                    "text_excerpt": "Mon-fri 7am to 1:30 pm\n\nBreakfast and Lunch\n\nThe grill team is good, Grill masters!\n\nCoffee is ok", 
                    "url": "http://www.yelp.com/biz/va-canteen-wla-2-los-angeles?hrid=G9Qr5OpQHs0qo89LFzYIGA", 
                    "user_name": "Patrick D.", 
                    "user_photo_url": "http://media1.fl.yelpcdn.com/upthumb/essl4VxDB599GHCamIdDdA/ms", 
                    "user_photo_url_small": "http://media1.fl.yelpcdn.com/upthumb/essl4VxDB599GHCamIdDdA/ss", 
                    "user_url": "http://www.yelp.com/user_details?userid=B7VkaAqckBslmw5HtstA1A"
                }
            ], 
            "state": "CA", 
            "state_code": "CA", 
            "url": "http://www.yelp.com/biz/va-canteen-wla-2-los-angeles", 
            "zip": "90073"
        }
    ], 
    "message": {
        "code": 0, 
        "text": "OK", 
        "version": "1.1.1"
    }
}
{
    "businesses": [], 
    "message": {
        "code": 0, 
        "text": "OK", 
        "version": "1.1.1"
    }
}
{
    "businesses": [], 
    "message": {
        "code": 0, 
        "text": "OK", 
        "version": "1.1.1"
    }
} 

我尝试了以下 R 代码:

library(dplyr)
library(plyr)
library(jsonlite) 

df <- fromJSON(paste(readLines("Yelp facility pretty print v2.txt"), collapse="")) 

但这只会返回第一个 JSON 对象。

然后我尝试了:

df <- fromJSON(sprintf("[%s]", paste(readLines("Yelp facility pretty print v2.txt"), collapse=",")))

但这会返回一个错误“ ...unexpected character ",";期望打开字符串quote(")作为键值。"

我验证了我的 JSON 文件中没有空行。非常感谢任何建议/帮助!

4

1 回答 1

0

您的问题是格式错误的 JSON,如本答案所述:https ://stackoverflow.com/a/34714966/5258043

您的输入 JSON 格式不正确,并且在根级别有多个元素。这类似于定义一个具有多个根的 XML 文档,当然这是不允许的。

读取文件的正确方法是:

my_data <- rjson::fromJSON(file='./yelp.txt')

但是,由于多个根元素,它失败了。您可以删除第一个元素之后的所有内容,也可以通过添加到文本文件的顶部和底部将所有内容包装在一个大根中,并用逗号分隔每个条目,以便 JSON 中的每个条目都是其自己的列表元素。

注意:你可以使用jsonlite包,我使用rjson它是因为它的默认解析创建了一个更好的列表,但你真的可以使用任何一个包。这是你的偏好。

于 2017-04-25T02:57:11.307 回答