I get JSON response when I hit wiki API as shown below. I find it complex to decode it.
{
"continue": {
"picontinue": 452160,
"continue": "||pageterms"
},
"query": {
"pages": [
{
"pageid": 164053,
"ns": 0,
"title": "Abdullah II of Jordan",
"index": 2,
"terms": {
"description": [
"King of the Hashemite Kingdom of Jordan"
]
}
},
{
"pageid": 348097,
"ns": 0,
"title": "Abdullah Ahmad Badawi",
"index": 9,
"terms": {
"description": [
"Malaysian politician"
]
}
},
{
"pageid": 385658,
"ns": 0,
"title": "Abdelaziz Bouteflika",
"index": 8,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Abdelaziz_Bouteflika_casts_his_ballot_in_May_10th%27s_2012_legislative_election_%28cropped%29.jpg/37px-Abdelaziz_Bouteflika_casts_his_ballot_in_May_10th%27s_2012_legislative_election_%28cropped%29.jpg",
"width": 37,
"height": 50
},
"terms": {
"description": [
"President of Algeria"
]
}
},
{
"pageid": 452160,
"ns": 0,
"title": "Abdul Qadeer Khan",
"index": 7,
"terms": {
"description": [
"Pakistani nuclear scientist"
]
}
},
{
"pageid": 2028438,
"ns": 0,
"title": "Abdelbaset al-Megrahi",
"index": 6,
"terms": {
"description": [
"Libyan mass murderer"
]
}
},
{
"pageid": 4709709,
"ns": 0,
"title": "Abdul",
"index": 1,
"terms": {
"description": [
"family name"
]
}
},
{
"pageid": 18950786,
"ns": 0,
"title": "Abdul Hamid II",
"index": 5,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Abdul_Hamid_2.jpg/35px-Abdul_Hamid_2.jpg",
"width": 35,
"height": 50
},
"terms": {
"description": [
"34th sultan of the Ottoman Empire"
]
}
},
{
"pageid": 19186951,
"ns": 0,
"title": "Abdullah of Saudi Arabia",
"index": 4,
"terms": {
"description": [
"former King of Saudi Arabia"
]
}
},
{
"pageid": 25955055,
"ns": 0,
"title": "Abdullah of Pahang",
"index": 10,
"terms": {
"description": [
"Sultan of Pahang"
]
}
},
{
"pageid": 36703624,
"ns": 0,
"title": "Abdel Fattah el-Sisi",
"index": 3,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Abdel_Fattah_el-Sisi_in_2017.jpg/39px-Abdel_Fattah_el-Sisi_in_2017.jpg",
"width": 39,
"height": 50
},
"terms": {
"description": [
"Current President of Egypt"
]
}
}
]
}
}
I am interested only in array of WikiPage data from the above JSON where structure should look something like this.
struct Wikipage: Decodable {
var pageid: Int?
var thumbnail: String?
var title: String?
var description: String?
enum CodingKeys: String, CodingKey {
case query
case pages
case terms
case description
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let query = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .query)
print(query)
}
}
I am not able to dig further into query
here as I get a data mismatch error. Can I be able to decode without using extra classes/vars to hold unwanted data? How do we decode in this case?
let wiki = try decoder.decode([Wikipage].self, from: data!)