Home > Net >  Iterate through JSON Object in Swift
Iterate through JSON Object in Swift

Time:08-16

I am trying to iterate through a JSON response that is full of arrays of flights - within those arrays are dictionaries. I am trying to loop through them all and just print them to the console for debugging but can't quite get the right answer. The reason I need to iterate is because eventually each flight will occupy a table view Cell

Below is my Structure for the response:

struct FlightData: Codable {
    let scheduled_arrivals: [Scheduled_Arrivals]?
}

struct Scheduled_Arrivals: Codable {
    let ident: String?
    let ident_icao: String?
    let ident_iata: String?
    let fa_flight_id: String
    let `operator`: String?
    let operator_icao: String?
    let flight_number: String?
    let registration: String?
    let atc_ident: String?
    let codeshares: [String]?
    let codeshares_iata: [String]?
    let blocked: Bool?
    let diverted: Bool?
    let cancelled: Bool?
    let position_only: Bool?
    let origin: Origin?
    let destination: Destination?
    let departure_delay: Int?
    let arrival_delay: Int?
    let filed_ete: Int?
    let scheduled_out: String?
    let estimated_out: String?
    let actual_out: String?
    let scheduled_off: String?
    let estimated_off: String?
    let actual_off: String?
    let scheduled_on: String?
    let estimated_on: String?
    let actual_on: String?
    let scheduled_in: String?
    let estimated_in: String?
    let actual_in: String?
    let progess_percent: Int?
    let status: String?
    let aircraft_type: String?
    let route_distance: Int?
    let filed_airspeed: Int?
    let filed_altitude: Int?
    let route: String?
    let baggage_claim: String?
    let seats_cabin_business: Int?
    let seats_cabin_coach: Int?
    let seats_cabin_first: Int?
    let gate_origin: String?
    let gate_destination: String?
    let terminal_origin: String?
    let terminal_destination: String?
    let type: String?
}

struct Origin: Codable {
    let code: String?
    let code_icao: String?
    let code_iata: String?
    let code_lid: String?
    let airport_info_url: String?
    
}

struct Destination: Codable {
    let code: String?
    let code_icao: String?
    let code_iata: String?
    let code_lid: String?
    let airport_info_url: String?
    
}

Below is my JSON Decoding - This is where I do not know how I can begin to loop through the values - You'll see I was trying to figure it out.

func parseJSON(_ flightDataPassedIn: Data) -> FlightModel? {

        let decoder = JSONDecoder()
        do {

           // let decodedData = try? decoder.decode(FlightDataArray.self, from: flightDataPassedIn)
            let decodedData = try? decoder.decode([String:FlightData].self, from: flightDataPassedIn)
            for (key, value) in decodedData ?? [:] {
                //What to Do here??
            }

CodePudding user response:

you could try something like this approach ...to loop through them all and just print them to the console ...:

func parseJSON(_ flightDataPassedIn: Data) -> FlightData? {
    do {
        let decodedData = try JSONDecoder().decode(FlightData.self, from: flightDataPassedIn)
        // print some info
        if let arrivals = decodedData.scheduled_arrivals {
            arrivals.forEach {
                print("flight_number: \($0.flight_number ?? "")")
                print("status: \($0.status ?? "")")
                print("origin: \($0.origin?.code ?? "")")
                // ....
            }
        }

        // print everything
        print("\n---> scheduled_arrivals: \(decodedData.scheduled_arrivals) \n")

        return decodedData
    } catch {
        print("error parsing:  \(error)")
    }
    return nil
}
  • Related