2 years ago
#32772

nitpaxy
keyNotFound decodable error while parsing a JSON object
Function call:
func getdata() {
let baseURL = “my url“
let token = “my token”
var urlRequest = URLRequest(url: URL(string: baseURL)!)
urlRequest.addValue("bearer \(token)", forHTTPHeaderField: "Authorization")
urlRequest.httpMethod = "GET"
let task = URLSession.shared
task.dataTask(with: urlRequest) { data, response, error in
if (error == nil && data != nil && data?.count != 0) {
let decoder = JSONDecoder()
do {
let result = try decoder.decode(Root.self, from: data!)
print(result.result.workStreams.workGroupObjects)
} catch let error {
debugPrint("error occurred while decoding: \(error)")
}
} else {
return
}
}.resume()
}
error in the console : "error occured while decoding: keyNotFound(CodingKeys(stringValue: "result", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"result\", intValue: nil) (\"result\").", underlyingError: nil))"
Models:
// MARK: - Root
struct Root: Decodable {
let result: Result
let status: Status
}
// MARK: - Result
struct Result: Decodable {
// let reportList: [Any?]
let userLayOut: String
let workStreams: WorkStreams
}
// MARK: - WorkStreams
struct WorkStreams: Decodable {
let workGroupObjects: String
// let ownerWorkGroupList: [Any?]
}
// MARK: - Status
struct Status: Decodable {
let number: Int
// let message: NSNull
}
am i missing something in my models? if yes what is it??
JSON:
{
"result": {
"ReportList": [],
"UserLayOut": “ This is a string ”,
"workStreams": {
"workGroupObjects": “ This is also a string ”,
"OwnerWorkGroupList": []
}
},
"Status": {
"Number": 0,
"Message": null
}
}
can someone please explain me what should i am i doing wrong?
swift
codable
decodable
0 Answers
Your Answer