通过在 r 中发出 get 请求从 JSON 文件中提取信息 [英] Extract information from JSON file by making get requests in r

查看:41
本文介绍了通过在 r 中发出 get 请求从 JSON 文件中提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发出一个 get 请求,并在 JSON 中解释它,但我不能.我认为这是因为swagger"是一个 HTTP 响应,但我不知道如何解析.

I am trying to make a get request, and interpret this in JSON but I cannot. I think this is because "swagger" is an HTTP response, but I don't know how to parse this.

library(RCurl)
library(rjson)


swagger = RCurl:: getURL(
  "https://requestresponse001.cloudapp.net:443/workspaces/7e8f135f31274b7eac419bd056875c03/services/a5b003e52c924d16a2e38ade45dd0154/swagger.json?api-version=2.0", 
  .opts = list(ssl.verifyHost = FALSE,ssl.verifyPeer = FALSE,followlocation=TRUE),header = "Accept: application/json", .encoding = "UTF-8"
)
# formatswagger <- jsonlite::toJSON(jsonlite::fromJSON(swagger), pretty = TRUE)

rjson::fromJSON(paste(readLines(swagger[1]), collapse=""))

我需要来自 JSON 文档的输入架构,它看起来像这样

I need the input schema from a JSON document that looks something like this

  "example": {
    "Inputs": {
      "input1": [
        {
          "Class": 1.0,
          "sepal-length": 1.0,
          "sepal-width": 1.0,
          "petal-length": 1.0,
          "petal-width": 1.0
        }
      ]
    },
    "GlobalParameters": {}
  }

推荐答案

你真的很接近.我更喜欢使用 httr(隐式使用 jsonlite):

You're really close. I'd prefer using httr (which implicitly uses jsonlite):

library(jsonlite)
library(httr)

# It's a self-signed cert
set_config(config(ssl_verifypeer=0L, ssl_verifyhost=0L), override=TRUE)
resp <- GET("https://requestresponse001.cloudapp.net:443/workspaces/7e8f135f31274b7eac419bd056875c03/services/a5b003e52c924d16a2e38ade45dd0154/swagger.json?api-version=2.0")

# will parse automagically
swagger <- content(resp)

str(swagger$definitions$ExecutionRequest$example)

## List of 2
##  $ Inputs          :List of 1
##   ..$ input1:List of 1
##   .. ..$ :List of 5
##   .. .. ..$ Class       : num 1
##   .. .. ..$ sepal-length: num 1
##   .. .. ..$ sepal-width : num 1
##   .. .. ..$ petal-length: num 1
##   .. .. ..$ petal-width : num 1
##  $ GlobalParameters: Named list()

这篇关于通过在 r 中发出 get 请求从 JSON 文件中提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆