json - 需要帮助在 SWIFT 中解析 wikipedia json api

标签 json swift api uiimageview wikipedia-api

下面是 WIKI 页面的 json。 (格式可能有很大偏差,所以这里有一个链接,可以更好地看到:https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=John_F._Kennedy&pithumbsize=500)

{ "batchcomplete": "", "query": { "normalized": [ { "from": "John_F._Kennedy", "to": "John F. Kennedy" } ], "pages": { "5119376": { "pageid": 5119376, "ns": 0, "title": "John F. Kennedy", "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/John_F._Kennedy%2C_White_House_photo_portrait%2C_looking_up.jpg/385px-John_F._Kennedy%2C_White_House_photo_portrait%2C_looking_up.jpg", "width": 385, "height": 500 }, "pageimage": "John_F._Kennedy,_White_House_photo_portrait,_looking_up.jpg" } } } }

我创建了结构来表示 json 键,但我不知道可以编写什么结构或 var 来表示数字 5119376,这显然是一个对象。所有其他键值都是字符串并且不会改变。但这个键很奇怪,因为它是一个整数,并且它在不同的维基页面之间变化。所以我不知道该给该键命名什么,因为它应该是一个数字,并且它会随着每个页面的变化而变化。

我尝试打印整个对象以查看它在控制台中的外观:

{ WikiStruct(query: quote_project.QueryStruct(pages: Optional(quote_project.PageStruct(pageid: nil, ns: nil, title: nil, thumbnail: nil, pageimage: nil))), batchcomplete: Optional("")) }

因此计算机得到“batchcomplete 是一个空字符串””,并且它还拾取了标准化的内容。但它说奇怪的 5119376 对象内的所有内容都是 nil。顺便说一下,我的最终目标是访问提供 wiki 页面主 jpg 图像的 url

我得到的错误是这样的:

 Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

我的结构如下。我尝试过忽略 5119376 对象并尝试为其创建某种结构,但无济于事

struct WikiStruct: Decodable {

var query: QueryStruct
var batchcomplete: String?

}

struct QueryStruct: Decodable {

var normalized: [NormalizedStruct]?
var pages: PageStruct?

}

struct NormalizedStruct: Decodable{

var from: String?
var to: String?
}

struct PageStruct: Decodable{

var pageid: Int?
var ns: Int?
var title: String?
var thumbnail: ThumbStruct?
var pageimage: String?

}

struct ThumbStruct: Decodable{

var source: String?//this is what I want
var width: Int?
var height: Int?

}

最佳答案

今天遇到了完全相同的问题。设法解决了它。基本上,嵌套在pages下的数据是一个带键的对象,键为5119376。要解析它,只需将QueryStruct中的pages变量更改为字典即可。像这样:

struct QueryStruct: Decodable {

var normalized: [NormalizedStruct]?
var pages: [String:PageStruct]?

}

之后我就可以成功解析该对象了。打印 WikiStruct 对象:

WikiStruct(查询:QueryStruct(规范化:[NormalizedStruct(从:“John_F._Kennedy”,到:“约翰·肯尼迪”)],页面:[“5119376”:PageStruct(pageid:5119376,ns :0,标题:“约翰·肯尼迪”,缩略图:ThumbStruct(来源:“https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/John_F._Kennedy%2C_White_House_color_photo_portrait.jpg/385px-John_F ._Kennedy%2C_White_House_color_photo_portrait.jpg",宽度:385,高度:500),页面图像:"John_F._Kennedy,_White_House_color_photo_portrait.jpg")]),批量完成:"")

关于json - 需要帮助在 SWIFT 中解析 wikipedia json api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415777/

相关文章:

javascript - 从动态创建的 div 动态创建 div

json - Swift - 从 Firebase 读取数据(实时数据库)

ios - 转换为 swift 3 后, View Controller 中出现奇怪的通用函数

swift - 为什么要小写uuid?

api - 使用 Youtube API 获取视频的隐私设置

javascript - 使用 Node 签署 AWS API Gateway 请求

json - 戈朗 : Type [type] is not an expression; json config parsing

ios - 扫描仪 View 中的导航栏 - IOS - Swift

java - 如何处理这个 OkHttp header 响应?

java - 如何解析Get Request Response?