使用RJSONIO从JSON http源中提取变量时出现问题

时间:2012-12-13 14:01:53

标签: json r null openstreetmap rjsonio

我正在尝试检索城市的经度和纬度。我创建了一个通过openstreetmap.org使用JSON的脚本。

library("RJSONIO")
CityName <- "Rotterdam"
CountryCode <- "NL"
CityName <- gsub(' ','%20',CityName)

url <- paste(
  "http://nominatim.openstreetmap.org/search?city="
      , CityName
      , "&countrycodes="
      , CountryCode
      , "&limit=1&format=json"
    , sep="")
x <- fromJSON(url,simplify=FALSE)

x
[[1]]
[[1]]$place_id
[1] "98036666"

[[1]]$licence
[1] "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright"

[[1]]$osm_type
[1] "relation"

[[1]]$osm_id
[1] "324431"

[[1]]$boundingbox
[[1]]$boundingbox[[1]]
[1] "51.842113494873"

[[1]]$boundingbox[[2]]
[1] "52.0045318603516"

[[1]]$boundingbox[[3]]
[1] "3.94075202941895"

[[1]]$boundingbox[[4]]
[1] "4.60184574127197"


[[1]]$lat
[1] "51.9228958"

[[1]]$lon
[1] "4.4631727"

[[1]]$display_name
[1] "Rotterdam, Stadsregio Rotterdam, Zuid-Holland, Nederland"

[[1]]$class
[1] "boundary"

[[1]]$type
[1] "administrative"

[[1]]$icon
[1] "http://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png"

我想提取$lon$lat,但是当我这样做时,我收到以下错误消息:

x$lon
NULL
x$lat
NULL

有没有人知道我做错了什么,因此没有得到预期的结果,如下所示:

x$lon
4.4631727
x$lat
51.9228958

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您需要先访问第一个列表:

> x[[1]]$lat
[1] "51.9228958"
> x[[1]]$lon
[1] "4.4631727"