如何在R中读取JSON API数据时处理嵌套标记?

时间:2015-06-26 14:32:55

标签: json r api web-scraping

我正在阅读Yelp API数据。我尝试使用Fromjso阅读它,但它无法正常工作。我是R的新手,你能帮我在data.frame中读取yelpdata吗?这看起来像嵌套标签给我。我希望每个标签看起来像列名。以下是我的代码

    yelpurl<- api_url
    yelpdata <- readLines(yelpurl)
    json_data <- fromJSON(yelpdata)
    yelpframe<- data.frame(json_data)
    head(yelpframe)

Yelpdata看起来像

    > yelpdata[[1]][[1]]
    [1] "{\"region\": {\"span\": {\"latitude_delta\": 0.0, \"longitude_delta\": 0.0}, \"center\": {\"latitude\": 41.9453333258613, \"longitude\": -87.6459314569375}}, \"total\": 4212, \"businesses\": [{\"is_claimed\": true, \"rating\": 4.5, \"mobile_url\": \"http://m.yelp.com/biz/si-pie-pizzeria-chicago\", \"rating_img_url\": \"http://s3-media2.fl.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png\", \"review_count\": 107, \"name\": \"Si-Pie Pizzeria\", \"snippet_image_url\": \"http://s3-media3.fl.yelpcdn.com/photo/EuuO7b8RZaubeJo0ZVzOuw/ms.jpg\", \"rating_img_url_small\": \"http://s3-media2.fl.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png\", \"url\": \"http://www.yelp.com/biz/si-pie-pizzeria-chicago\", \"menu_date_updated\": 1434364983, \"phone\": \"7733601862\", \"snippet_text\": \"Really good NY style pizza. Awesome service.  \\n\\nThe pizza crust was crispy but chewy (as NY style should be), sauce tangy and toppings on both the sausage...\", \"image_url\": \"http://s3-media2.fl.yelpcdn.com/bphoto/ZNJ-cIaL2ovLXBUt14kBng/ms.jpg\", \"categories\": [[\"Pizza\", \"pizza\"]], \"display_phone\": \"+1-773-360-1862\", \"rating_img_url_large\": \"http://s3-media4.fl.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png\", \"menu_provider\": \"single_platform\", \"id\": \"si-pie-pizzeria-chicago\", \"is_closed\": false, \"location\": {\"cross_streets\": \"Stratford Pl \\u0026 Cornelia Ave\", \"city\": \"Chicago\", \"display_address\": [\"3477 N Broadway ST\", \"Lakeview\", \"Chicago, IL 60657\"], \"geo_accuracy\": 9.5, \"neighborhoods\": [\"Lakeview\"], \"postal_code\": \"60657\", \"country_code\": \"US\", \"address\": [\"3477 N Broadway ST\"], \"coordinate\": {\"latitude\": 41.9453333258613, \"longitude\": -87.6459314569375}, \"state_code\": \"IL\"}}]}"

Yelpframe看起来像

> head(yelpframe)
  region.span.latitude_delta region.span.longitude_delta region.center.latitude region.center.longitude total
1                          0                           0               41.94533               -87.64593  4212
  businesses.is_claimed businesses.rating                         businesses.mobile_url
1                  TRUE               4.5 http://m.yelp.com/biz/si-pie-pizzeria-chicago
                                                                    businesses.rating_img_url businesses.review_count
1 http://s3-media2.fl.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png                     107
  businesses.name                                        businesses.snippet_image_url
1 Si-Pie Pizzeria http://s3-media3.fl.yelpcdn.com/photo/EuuO7b8RZaubeJo0ZVzOuw/ms.jpg
                                                                    businesses.rating_img_url_small
1 http://s3-media2.fl.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png
                                   businesses.url businesses.menu_date_updated businesses.phone
1 http://www.yelp.com/biz/si-pie-pizzeria-chicago                   1434364983       7733601862
                                                                                                                                          businesses.snippet_text
1 Really good NY style pizza. Awesome service.  \n\nThe pizza crust was crispy but chewy (as NY style should be), sauce tangy and toppings on both the sausage...
                                                  businesses.image_url businesses.categories businesses.display_phone
1 http://s3-media2.fl.yelpcdn.com/bphoto/ZNJ-cIaL2ovLXBUt14kBng/ms.jpg          Pizza, pizza          +1-773-360-1862
                                                                    businesses.rating_img_url_large businesses.menu_provider
1 http://s3-media4.fl.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png          single_platform
            businesses.id businesses.is_closed businesses.location.cross_streets businesses.location.city
1 si-pie-pizzeria-chicago                FALSE       Stratford Pl & Cornelia Ave                  Chicago
              businesses.location.display_address businesses.location.geo_accuracy businesses.location.neighborhoods
1 3477 N Broadway ST, Lakeview, Chicago, IL 60657                              9.5                          Lakeview
  businesses.location.postal_code businesses.location.country_code businesses.location.address
1                           60657                               US          3477 N Broadway ST
  businesses.location.coordinate.latitude businesses.location.coordinate.longitude businesses.location.state_code
1                                41.94533                                -87.64593                             IL

Yelpframe没有采用正确的列行格式。

0 个答案:

没有答案