skip to Main Content

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


  1. Chosen as BEST ANSWER

    Managed to get it to work by using the above changes and then adding the below

       @State private var jobRecordId = JobById(id: "", jobUniqid: "", jobClientJobNo: "", jobPriority: "", jobQuoteNo: "", jobInvoiceNo: "", jobStatus: "", jobAgentId: "", jobAddr1: "", jobAddr2: "", jobTownCity: "", jobCounty: "", jobPostcode: "", jobContactName1: "", jobContactNo1: "", jobContactEmail1: "", jobContactName2: "", jobContactNo2: "", jobContactEmail2: "", jobSentBy: "", jobType: "", jobContractorId: "", jobDescription: "", jobDescriptionShort: "", jobBudget: "", jobContractorBudget: "", jobCreatedDate: "", jobAgentNotes: "", jobContractorNotes: "", job_dynamo_notes: "", job_notes: "", job_contractor_instructed: "", job_contractor_action: "", job_contractor_accepted: "", job_contractor_accepted_date: "", job_contractor_description: "", job_contractor_price_agreed: "", job_booked_date: "", job_quoted_date: "", job_accepted_date: "", job_start_date: "", job_completion_date: "", job_invoice_date: "", job_onhold_date: "", job_cancelled_date: "", job_total_quote: "", job_contractor_cut_pre_vat: "", job_contractor_cut: "", job_dynamo_cut: "", job_agents_cut: "", job_sophie_cut: "", job_invoice_amount: "", job_pre_vat_amount: "", job_net_amount: "", job_vat_amount: "", job_gsc_expiry_date: "", job_floor_plan: "", job_vat_charged: "", job_vat_collected: "", job_invoice_notes: "", job_contractor_paid: "", job_invoice_paid: "", job_invoice_paid_email: "", job_agents_vat: "", job_vat_liability: "", job_contractor_invoice: "", job_contractor_invoice_no: "", job_contact_reason: "", job_quote_description: "", job_contractor_proceed: "", job_contractor_proceed_receipt: "", job_send_sms: "", job_details_of_gsc: "", job_details_id: "", job_introducer_id: "", job_introducer_commission_paid: "", job_eicr_expiry_date: "", job_eicr_expiry_info: "", job_epc_expiry_info: "", job_gsc_expiry_info: "", job_epc_expiry_date: "", job_onhold_reason: "", job_part_payment_received: "", job_contractor_part_payment_paid: "", job_contractor_balance: "", job_quote_rejected_date: "", job_reminder_sent: "", job_deposit_invoiced: "", job_amber_list: "")
    

  2. 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

     let document: [JobById]
    

    with this

    let document: JobById
    

    and also change this

    @State var jobRecordId = JobById
    

    with

    @State var jobRecordId : JobById
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search