【发布时间】:2019-01-06 01:54:36
【问题描述】:
我正在使用json_serializable pub package 来满足我所有的 RESTful API 需求,而且效果很好,但现在我正在尝试序列化来自 Wikipedia 的内容。
我的问题是响应结构,其中一个键是预先未知的,并且根据返回的页面 ID 是动态的,这是一个示例:
我只想获取页面的介绍部分,我的查询是: https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&redirects=1&exintro=1&explaintext=1&titles=Stack%20Overflow
响应将是:
{
"batchcomplete": "",
"query": {
"pages": {
"21721040": {
"pageid": 21721040,
"ns": 0,
"title": "Stack Overflow",
"extract": "Stack Overflow is a privately held website, the flagship site of ......."
}
}
}
}
最终我只想获得内部extract 字符串,但“在路上”我在pages 下有页面ID。
当我不知道关键是什么时,我找不到如何获取对象,既不使用json_serializable package,也不明确编写 JSON 导入代码。有可能吗?
PS,我确实认为我找到了一种需要两次 API 调用的方法 - 如果我添加 indexpageids 标志并将其设置为 true,i will receive an additional dictionary entry called pageids with the page id numbers as string,那么我可以在第二次 API 调用。
我仍然不知道我会做的确切方式,但我很确定这是可能的,但每次发送 2 个 API 请求很昂贵,我想知道是否有更优雅的方式来做.
【问题讨论】:
-
你不能使用
Map<String, Page>作为pages的类型吗?
标签: json rest api dart flutter