【发布时间】:2021-10-16 05:32:48
【问题描述】:
我有一个看起来像这样的脚本(虽然不完全是因为我显然不能提供我的 API 密钥以及什么不能使其完全可复制)
library(tidyverse)
library(dplyr)
library(httr)
library(civis)
library(rjson)
library(jqr)
record <- GET(
'https://api.secure.com/v4/data',
accept_json(),
add_headers(Accept = 'application/json',
Authorization = VanAPI)
)
record 然后生成一个看起来像这样的列表:
Response [https://api.secure.com/v4/data]
Date: 2021-08-12 20:54
Status: 200
Content-Type: application/json; charset=utf-8
Size: 873 B
{
"items": [
{
"playerId": 12827,
"name": "Player Tiers",
"type": "Dynamic",
"description": null,
"points": 249,
"areSubgroupsSticky": false,
"status": "Active",
...
将其转换为 JSON 以便我可以将 jqr 包应用到的最佳方法是什么?
我现在正在做:
test <- content(record)
产生这个输出:
$items
$items[[1]]
$items[[1]]$playerId
[1] 12827
$items[[1]]$name
[1] "Player Tiers"
$items[[1]]$type
[1] "Dynamic"
$items[[1]]$description
NULL
$items[[1]]$points
[1] 249
$items[[1]]$areSubgroupsSticky
[1] FALSE
$items[[1]]$status
[1] "Active"
$items[[1]]$subgroups
NULL
$items[[1]]$markedSubgroup
NULL
$items[[2]]
$items[[2]]$playerId
[1] 15723
$items[[2]]$name
[1] "Team Tiers"
$items[[2]]$type
[1] "Dynamic"
$items[[2]]$description
NULL
$items[[2]]$points
[1] 35
$items[[2]]$areSubgroupsSticky
[1] FALSE
$items[[2]]$status
[1] "Active"
$items[[2]]$subgroups
NULL
$items[[2]]$markedSubgroup
NULL
$items[[3]]
$items[[3]]$playerId
[1] 16620
$items[[3]]$name
[1] "Coaches Tiers"
$items[[3]]$type
[1] "Dynamic"
$items[[3]]$description
NULL
$items[[3]]$points
[1] 12
$items[[3]]$areSubgroupsSticky
[1] FALSE
$items[[3]]$status
[1] "Active"
$items[[3]]$subgroups
NULL
$items[[3]]$markedSubgroup
NULL
$nextPageLink
NULL
$count
[1] 3
然后我尝试使用test2 <- rjson::toJSON(test) 将其转换为 JSON,但这会产生:
[1] "{\"items\":[{\"playerId\":12827,\"name\":\"Player Tiers\",\"type\":\"Dynamic\",\"description\":null,\"points\":249,\"areSubgroupsSticky\":false,\"status\":\"Active\",\"subgroups\":null,\"markedSubgroup\":null},{\"playerId\":15723,\"name\":\"Team Tiers\",\"type\":\"Dynamic\",\"description\":null,\"points\":35,\"areSubgroupsSticky\":false,\"status\":\"Active\",\"subgroups\":null,\"markedSubgroup\":null},{\"playerId\":16620,\"name\":\"Coaches Tiers\",\"type\":\"Dynamic\",\"description\":null,\"points\":12,\"areSubgroupsSticky\":false,\"status\":\"Active\",\"subgroups\":null,\"markedSubgroup\":null}],\"nextPageLink\":null,\"count\":3}"
有没有更好的方法来获得简单、干净的 JSON 输出?
【问题讨论】: