2017-04-10 68 views
2

我有一个JSON URl - https:// {{API_HOST}}/api/dd/new需要在R中解析。 它有一个键和值,通过我可以轻松地通过应用标题部分中的键和值来解析Postman中的JSON。在R中使用Key和Value寻找一种方法。使用键和值解析R中的JSON

我知道如何使用基本身份验证来解析JSON。

代码:

library(jsonlite) 
library(httr) 

Url <- "https://{{API_HOST}}/api/dd/new" 

Url.Response <- GET(Url,authenticate("string","password")) 

RawJson <- rawToChar(Url.Response$content) 

JsonData <- fromJSON(RawJson) 

JsonDataDf <- as.data.frame(JsonData) 

R中寻找类似的方式来解析使用键和值的JSON的URL。 add_headers在这里会有所帮助。 让键STRING, 价值qwertzuiop

回答

1

能解决这个问题,以下是解决方案:

library(httr) 
library(jsonlite) 

getURL <- "https://{{API_HOST}}/api/dd/new" 

auth_key <- "qwertzuiop" 

RESTQuery = getURL 

RESTGETHeader = c(
'STRING'=auth_key, 
'Accept'="application/json" 
) 

RESTResult <- GET(RESTQuery,add_headers(RESTGETHeader)) 

RawJson <- rawToChar(RESTResult$content) 

JsonData <- fromJSON(RawJson) 

JsonDataDf <- as.data.frame(JsonData)