如何selectR中的特定部分的JSON数据?
我想从url中导入数据到JSON格式的R中,然后将其导出到Excel文件中。
url : https : //api.typeform.com/v1/form/JlCM2J? key =ecab3f590a2af4ca55468adc95686a043bbf6c9a
这是我的R代码
library(data.table) library(httr) library(rjson) set_config(config(ssl_verifypeer = 0L)) var1=fread('https://api.typeform.com/v1/form/JlCM2J?key=ecab3f590a2af4ca55468adc95686a043bbf6c9a') head(var1)
输出:
空的data.table(0行)的161列:{“http_status”:200,stats“:{”responses“:{”showing“:2,”total“:2,”completed“:1}}, “:[{”id“:”textfield_38991412,question“:”你叫什么名字,field_id“:38991412},{”id“:”statement_38991416,question“:”Hi {{answer_38991412}},谢谢这个问卷。你的答案将帮助我们为你打造一个伟大的品牌。一个在你的客户心中强大和令人难忘的。 一个明确定义你是什么,你代表什么,什么使你有所不同。让我们开始!,field_id“:38991416},{”id“:”group_38991407 …
需要:我只需要将这些数据的responses
部分导出为Excel文件。
整个数据可以通过在jsonviewer.stack.hu
网站上粘贴上面的url来查看
以下是在现代R中与REST API接口的更多(IMO)习惯用法:
library(httr) library(jsonlite) library(dplyr) res <- GET("https://api.typeform.com/v1/form/JlCM2J", query=list(key="ecab3f590a2af4ca55468adc95686a043bbf6c9a")) content(res, as="text") %>% fromJSON(flatten=FALSE) -> out glimpse(out$responses$answers) ## Observations: 2 ## Variables: 26 ## $ textfield_38991412 <chr> NA, "A" ## $ dropdown_38991418 <chr> NA, "Accounting" ## $ textarea_38991420 <chr> NA, "A" ## $ textfield_38991413 <chr> NA, "A" ## $ textarea_38991421 <chr> NA, "A" ## $ listimage_38991426_choice <chr> NA, "Company" ## $ textfield_38991414 <chr> NA, "A" ## $ website_38991435 <chr> NA, "http://A.com" ## $ textarea_38991422 <chr> NA, "A" ## $ listimage_38991427_choice <chr> NA, "Sincere" ## $ listimage_38991428_choice <chr> NA, "Male" ## $ list_38991436_choice <chr> NA, "17 or younger" ## $ list_38991437_choice <chr> NA, "Upper class" ## $ listimage_38991429_choice_49501105 <chr> NA, "Store" ## $ listimage_38991430_choice <chr> NA, "Product" ## $ textarea_38991423 <chr> NA, "A" ## $ listimage_38991431_choice <chr> NA, "Techy" ## $ listimage_38991432_choice_49501124 <chr> NA, "Fuchsia Rose" ## $ listimage_38991433_choice <chr> NA, "Classic" ## $ list_38991438_choice <chr> NA, "$3,000 or less" ## $ listimage_38991434_choice_49501140 <chr> NA, "Brand Design" ## $ textarea_38991424 <chr> NA, "A" ## $ textfield_38991415 <chr> NA, "A" ## $ dropdown_38991419 <chr> NA, "Afghanistan" ## $ email_38991439 <chr> NA, "A@a.com" ## $ textarea_38991425 <chr> NA, "A"
- 直接使用
httr::GET()
可以更容易地pipe理额外的参数。 - 使用
httr::content()
来获取响应并检索原始文本可以实现更细粒度的处理(如果需要的话) - 使用
jsonlite::fromJSON()
直接提供对单个JSON处理选项的更精细的控制。
然而,有一个R包rtypeform
,它真的简化了一切(有趣的事实:它遵循上面的封面):
library(rtypeform) library(dplyr) res <- get_results("JlCM2J") glimpse(res$responses$answers) ## Observations: 2 ## Variables: 26 ## $ textfield_38991412 <chr> NA, "A" ## $ dropdown_38991418 <chr> NA, "Accounting" ## $ textarea_38991420 <chr> NA, "A" ## $ textfield_38991413 <chr> NA, "A" ## $ textarea_38991421 <chr> NA, "A" ## $ listimage_38991426_choice <chr> NA, "Company" ## $ textfield_38991414 <chr> NA, "A" ## $ website_38991435 <chr> NA, "http://A.com" ## $ textarea_38991422 <chr> NA, "A" ## $ listimage_38991427_choice <chr> NA, "Sincere" ## $ listimage_38991428_choice <chr> NA, "Male" ## $ list_38991436_choice <chr> NA, "17 or younger" ## $ list_38991437_choice <chr> NA, "Upper class" ## $ listimage_38991429_choice_49501105 <chr> NA, "Store" ## $ listimage_38991430_choice <chr> NA, "Product" ## $ textarea_38991423 <chr> NA, "A" ## $ listimage_38991431_choice <chr> NA, "Techy" ## $ listimage_38991432_choice_49501124 <chr> NA, "Fuchsia Rose" ## $ listimage_38991433_choice <chr> NA, "Classic" ## $ list_38991438_choice <chr> NA, "$3,000 or less" ## $ listimage_38991434_choice_49501140 <chr> NA, "Brand Design" ## $ textarea_38991424 <chr> NA, "A" ## $ textfield_38991415 <chr> NA, "A" ## $ dropdown_38991419 <chr> NA, "Afghanistan" ## $ email_38991439 <chr> NA, "A@a.com" ## $ textarea_38991425 <chr> NA, "A"
无论哪种方式,如果您不习惯使用$
来访问列表中的字段,这必须是您第一次使用R(或者几乎是第一次)。 在尝试使用API数据之前,您应该花一些时间学习R。 不正确的结果和自我挫折是唯一可以通过编码来粘贴和祈祷的方式。 你仍然需要把它写入一个CSV文件( write.csv()
)。
倒数第二,如果你只是最终使用Excel,为什么你以编程方式检索表单响应? 如果你不打算把R用于剩下的工作,这似乎是一个不必要的步骤,除非你想脚本下载的数据与让某人login到网站来获取它。
最后,在您将其公开发布之后立即无效并重新生成您的API密钥 。 我现在知道你有两种forms:“品牌问卷调查”和“testingforms”,并将能够监视你在Typeform中做什么,并随时检索你的任何forms的数据。 rtypeform
软件包可让您将API密钥存储在typeform_api
环境variables中(您可以使用~/.Renviron
来保存这些数据),因此您~/.Renviron
再在脚本中将其暴露给世界。