Search code examples
swiftxcodeapialamofiredecodable

Decoding structure use by SwiftyJson and Alamofire


How can I decode data to a structure using Alamofire and SwiftyJSON? My attempts give me errors like that

"No value associated with key CodingKeys(stringValue: \"user\", intValue: nil)

Here is my code, my try doesn't give me the result when I use non-optional values, they respond to me with NIL values

Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if response.data != nil {
                switch response.result {
                case.failure( let error):
                    print(error)
                case.success(let val):
                    var json = JSON(val)
                    print(json)
                    guard let data = response.data else {return}
                    do {
                        let root = try JSONDecoder().decode(MainInfo.self, from: data)
                        print(root.submodel)
                    }
                    catch {
                        print("Bigerror")
                        print(error)
                    }

This is my structure

struct user: Codable {
    var push_id:String?
    var name:String?
    var id:String?
    var role_id:String?
    var taxi_park_id:Int?
    var car_number:String?

    enum CodingKeys:String,CodingKey {
        case push_id = "push_id"
        case name = "name"
        case id = "id"
        case role_id = "role_id"
        case taxi_park_id = "taxi_park_id"
        case car_number = "car_number"     
    }
}

struct MainInfo : Decodable {
    let model: String?
    let submodel: String?
    let user:user
    enum CodingKeys:String,CodingKey {
        case model = "model"
        case submodel = "submodel"
        case user = "user"

    }
}

This is my pretty printed json

{
  "facilities" : [

  ],
  "model" : "AMC",
  "taxi_park" : "Taxi +",
  "submodel" : "Gremlin",
  "user" : {
    "role_id" : 2,
    "push_id" : "dW7Cy-ItcDo:APA91bH62zJJKKz0t9VxP29H0iE2xhnQH0hDvKpGaHc5pknuTuZq2lMaj-EapQlN3O4dJF0ysSuCNOeb-2SdJaJaLIZcwHD3CCpeNpz6UVeGktoCm2ykL2rNXF5-ofQckvz1xTvVO0V6",
    "taxi_park_id" : 0,
    "id" : 3,
    "name" : "China",
    "car_number" : "X123OOO"
  }
}

Solution

  • Try this code, also a simple tip, we use coding keys in swift because sometimes we have to receive an inconvenient parameter keys but we also want to use it simple and clearly in the struct therefore CodingKeys are helpful in your case you using CodingKeys to decode the same parameter name i added the following taxiPark propriety to give you a hint on why they are useful, for example: i want to parse a JSON that have a key called

    Person_ID_From_School  
    

    with coding keys i can do that with a better naming simple as personId and so on

     struct MainInfo : Decodable {
            let model: String?
            let submodel: String?
            let user:user
            let taxiPark: String? 
            let facilities: [String?]?
            enum CodingKeys:String,CodingKey {
                case model = "model"
                case submodel = "submodel"
                case user = "user"
                case taxiPark = "taxi_park"
                case facilities = "facilities"
    
            }
        }