I am changing my results from an API call from SQL to the results from a MariaDB Stored Procedure. The resulting JSON is below but I don't know how to set up my Structs properly ( I'm guessing) and then the decode itself isn't correct.
Sorry but I've read and read and strugle to understand how to correct this.
I have the following:
JSON
[
[
{
"profile_id": 1,
"lost_id": 14,
"city": "",
"state": "",
"zip": "",
"quick_desc": "",
"comments": "",
"longitude": "-122.40641700000002",
"latitude": "37.78583399999998",
"contact_phone": "",
"image_id_1": 13,
"image_id_2": 0,
"image_id_3": 0,
"last_update_TS": "2024-08-25T14:49:16.000Z",
"location_desc": "",
"lost_datetime": "2024-08-25T14:49:16.000Z",
"status": "L",
"status_desc": "Lost",
"gender": "M",
"neutered": "N",
"userid": "kathylindsay",
"radius": 2000,
"distance": 1419.8404308667655
},
{
"profile_id": 1,
"lost_id": 15,
"city": "",
"state": "",
"zip": "",
"quick_desc": "",
"comments": "",
"longitude": "-122.40641700000002",
"latitude": "37.78583399999998",
"contact_phone": "",
"image_id_1": 14,
"image_id_2": 0,
"image_id_3": 0,
"last_update_TS": "2024-08-25T14:51:31.000Z",
"location_desc": "",
"lost_datetime": "2024-08-25T14:51:31.000Z",
"status": "L",
"status_desc": "Lost",
"gender": "M",
"neutered": "N",
"userid": "kathylindsay",
"radius": 2000,
"distance": 1419.8404308667655
}
],
{
"affectedRows": 0,
"insertId": 0,
"warningStatus": 0
}
]
Structs
struct SPResults: Codable {
var returned: [LPResult]
var affectedRows: Int
var insert_id: Int
var err: Int
}
struct LPResponse: Codable {
var results: [LPResult]
}
struct LPResult: Codable {
var profile_id: Int
var lost_id: Int
var city: String
var state: String
var zip: String
var quick_desc: String
var comments: String
var longitude: String
var latitude: String
var location_desc: String
var contact_phone: String
var image_id_1: Int
var image_id_2: Int
var image_id_3: Int
var status: String
var status_desc: String
var gender: String
var neutered:String
var lost_datetime: String
var userid: String
var radius: Int
var last_update_TS: String
var distance: Double
}
Code that throws and error of:
Error typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
Code:
let url: URL = URL(string: urlPath)!
// let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
//let task = defaultSession.dataTask(with: url) { (data, response, error) in
URLSession.shared.dataTask(with: url) { (data, response,err) in
guard let data = data else { return }
do {
let decodedResponse = try JSONDecoder().decode([SPResults].self, from: data)
// let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
DispatchQueue.main.async {
print(data)
// update our UI
//self.profile_ID = decodedResponse.LPResult[]
print(decodedResponse)
}
return
} catch let jsonErr {
print("Error", jsonErr)
}
}.resume()
Any help/direction to resolve would really be appreciated.
Thanks - Allan