如何从 API 中获取必要的数据

How to get the necessary data from an API

我需要使用以下 API 在 Oxygen 中制作一个学校项目:https://potterapi.com/ 我写了一个函数来从 API:

中获取所有必要的 JSON 数据

让$apikey := fn:doc("potterapi.key")/apikey/string() return 地图 { "characters": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey), "houses": fn:json-doc("https://www.potterapi.com/v1/houses?key=" || $apikey), "spells": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey) }

我还制作了另一个 xml 方案,我在其中定义了 apikey,我需要以 json 格式获取数据。 你能帮我解决这个问题吗?

要使用 XQuery 3.1 将 XQuery 3.1 映射序列化为 JSON,您有两个选择,要么使用序列化函数

serialize(
  let $apikey := fn:doc("potterapi.key")/apikey/string() return map { "characters": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey), "houses": fn:json-doc("https://www.potterapi.com/v1/houses?key=" || $apikey), "spells": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey) },
  map { 'method' : 'json', 'indent' : true() }
)

或使用所需的 XQuery 选项声明

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'json';
declare option output:indent 'yes';

let $apikey := fn:doc("potterapi.key")/apikey/string() return map { "characters": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey), "houses": fn:json-doc("https://www.potterapi.com/v1/houses?key=" || $apikey), "spells": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey) }