-1

How to parse the following response using Codable

{
    "Response": {
        "ResponseStatus": 1,`enter code here`
        "TraceId": "00125bf6-a416-4095-893c-05e05f8c7202",
        "Origin": "BOM",
        "Destination": "PNQ",
        "Results": [
                        [
                            {
                                "IsCouponAppilcable": true,
                                "IsGSTMandatory": false,
                                "AirlineRemark": "AI TEST.",
                            },
                            {
                                "IsCouponAppilcable": true,
                                "IsGSTMandatory": false,
                                "AirlineRemark": "AI TEST.",
                            }
                       ]
                ]
        }
}

I want to parse "Results" using Codable

I tried this but stuck how to parse "Results"

struct FlightResponceRequest : Codable {
    var Response : FlightResponce
}

struct FlightResponce : Codable {
    var ResponseStatus : Int?
    var Error : FlightError
    var TraceId : String?
    var Origin : String?
    var Destination : String?
    var Results : [FlightResult]?
}

struct FlightError : Codable {
    var ErrorCode : Int?
    var ErrorMessage : String?
} 

struct FlightResult : Codable {

}
koen
  • 5,383
  • 7
  • 50
  • 89
  • 3
    Please make an attempt to solve this yourself first and then we can help you with that if you get stuck. – Joakim Danielson Oct 18 '19 at 13:13
  • Add the code to your question instead of posting it as a comment and explain what the issue is with it, – Joakim Danielson Oct 18 '19 at 13:21
  • 1
    Show the code where you use `JSONDecoder()`. Show the error that you catch I guess? Like this code minimum: `do { let result = try JSONDecoder().decode(FlightResponceRequest.self, from: json); print(result) } catch { print("Error: \(error)") }` and ALSO the log in console... It would save us a lot of time, and that's your part to do at least as a developer to catch error, and read them... – Larme Oct 18 '19 at 13:37
  • Hi Larme, do { Flights.sharedInstance.flightObj = try JSONDecoder().decode(FlightResponceRequest.self, from: data) } If you check my struct, I haven't written anything in FlightResult? My question is what I Write In FlightResult to Parse "Result" – Sagar Shinde Oct 18 '19 at 13:40
  • 1
    In your response I don't see FlightError field, so try to make it optional – V.V.V Oct 18 '19 at 14:06

1 Answers1

1

You need to make it like that for response because you have an array inside another array.

var Results : [[FlightResult]]?

And then parse key/values to your FlightResult structure.

bisma
  • 795
  • 6
  • 26