Have been trying to figure this out for the last 3+ days, I’m trying to pull Json from a URL that returns the following
I’ve been trying to figure it out for myself but I haven’t made any progress, so was hoping the community can help me.
{
"status": "success",
"code": 1,
"message": "dp_jobs found",
"document": {
"ID": "013",
"job_uniqid": "d8d12bed02e84bad462090b51bcdfcd5",
"job_client_job_no": "",
"job_priority": "",
"job_quote_no": "",
"job_invoice_no": "-",
"job_status": "5",
"job_agent_id": "18",
"job_addr_1": "",
"job_addr_2": "",
"job_town_city": "",
"job_county": "",
"job_postcode": "",
"job_contact_name_1": ""
.......
}
}
I’m feel like its the below code but could be wrong!
// MARK: - Job ID
struct JobIdAPI: Codable {
let status: String
let code: Int
let message: String
let document: [JobById]
}
struct JobById: Codable, Identifiable {
let id: String
let jobUniqid: String?
let jobClientJobNo: String?
let jobPriority: String?
let jobQuoteNo: String?
let jobInvoiceNo: String?
let jobStatus: String?
let jobAgentId: String?
let jobAddr1: String?
let jobAddr2: String?
let jobTownCity: String?
let jobCounty: String?
let jobPostcode: String?
let jobContactName1: String?
........
enum CodingKeys: String, CodingKey {
case id = "ID"
case jobUniqid = "job_uniqid"
case jobClientJobNo = "job_client_job_no"
case jobPriority = "job_priority"
case jobQuoteNo = "job_quote_no"
case jobInvoiceNo = "job_invoice_no"
case jobStatus = "job_status"
case jobAgentId = "job_agent_id"
case jobAddr1 = "job_addr_1"
case jobAddr2 = "job_addr_2"
case jobTownCity = "job_town_city"
case jobCounty = "job_county"
case jobPostcode = "job_postcode"
case jobContactName1 = "job_contact_name_1"
case jobContactNo1 = "job_contact_no_1"
case jobContactEmail1 = "job_contact_email_1"
case jobContactName2 = "job_contact_name_2"
.........
}
}
EDITED: 25th May 23
@State var jobRecordId = JobById
func loadData() async {
guard let url = URL(string: "https://api.badsey.app/(MemberCheck)/API/dp_jobs/read_one.php?id=(LastJobID)") else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer (TokenCheck)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request) { data, response, error in
//print("---> data: (String(data: data!, encoding: .utf8) as AnyObject)")
do {
let decodedResponse = try JSONDecoder().decode(JobIdAPI.self, from: data!)
DispatchQueue.main.async {
self.jobRecordId = decodedResponse.document
}
} catch {
print(error)
}
}.resume()
}
The result should be a single json response, but I get the following error
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "document", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
I know it’s telling me the error and what it expected, but I just can’t seem to get the result I’m after
2
Answers
Managed to get it to work by using the above changes and then adding the below
Your model class has some minor mistakes. As your error says,
Expected to decode Array<Any> but found a dictionary instead
You need to change your array type with the dictionary type
with this
and also change this
with