我正在使用 R 中的 httr 包来尝试查询 postcode.io API (http://postcodes.io/docs).
我可以按照说明成功查询单个邮政编码:sample4 <- GET("api.postcodes.io/postcodes/EN14RF")
当我尝试查询多个邮政编码时,我变得有点困惑。 postcode.io 说明建议
POST https://api.postcodes.io/postcodes?q=[postcode]
其中指定了包含邮政编码数组的 JSON 对象。我有一个包含邮政编码的 R 向量,我尝试将其转换为 JSON 对象,如下所示:
a <- toJSON(a)
我的 R 向量 'a' 是:
structure(c(4L, 5L, 3L, 6L, 1L, 2L), .Label = c("Bn14 9aw", "CR0 4BE", "E5 8HB", "EN1 4RF", "G42 8QN", "SA1 3UL"), class = "factor")
现在,当我尝试使用以下代码行查询 API 时:
sample4 <- POST("https://api.postcodes.io/postcodes?q=[postcode]", body = list(postcode = add1JSON))
我收到错误:"Invalid JSON submitted. You need to submit a JSON object with an array of postcodes or geolocation objects"
我有一种感觉,这是因为我没有提供数组,而是提供未命名的列表,例如我的 JSON 对象应该如下所示:
{"postcodes":"[ \"EN14RF\", \"G428QN\", \"E58HB\", \"SA13UL\", \"Bn149aw\", \"CR04BE\" ]"}
不是这个:"[ \"EN14RF\", \"G428QN\", \"E58HB\", \"SA13UL\", \"Bn149aw\", \"CR04BE\" ]"
谁能帮我这个?我有一种感觉,这与我的toJSON
调用,但在论坛或 API 开发人员页面上找不到类似的示例:(
非常感谢
Marty