Swift 4 + Alamofire Decodable Json URL format

Ümit Sevim picture Ümit Sevim · Jan 19, 2018 · Viewed 8.8k times · Source

I have a JSON format which I do not decodable with Alamofire.

Here is my json:

"data":[  
{  
    "id":37,
    "status":"A\u00e7\u0131k",
    "department":"Muhasebe",
    "title":"Y\u00f6netim Panelinden Deneme 4 - Mail Kontrol",
    "message":"<p>Y\u00f6netim Panelinden Deneme 4 - Mail Kontrol<br><\/p>",
    "file":null,
    "created_at":{  
        "date":"2018-01-13 01:59:49.000000",
        "timezone_type":3,
        "timezone":"UTC"
    },
    "replies":[  
        {  
            "id":6,
            "ticket_id":37,
            "admin_id":null,
            "user_id":8593,
            "message":"<p>test<\/p>",
            "file":"uploads\/tickets\/8593-P87wd8\/GFV6H5M94y5Pt27YAxZxHNRcVyFjD554i80og3xk.png",
            "created_at":"2018-01-18 11:16:55",
            "updated_at":"2018-01-18 11:16:55"
        }
    ]
},

Here is my model for the JSON:

struct TeknikDestek : Decodable {
    var id: Int?
    var status: String?
    var title: String?
    var department: String?
    var message: String?

    var replies: [Replies]?
}

struct Replies: Decodable {
    var replyid: Int?
    var ticket_id: Int?
    var admin_id: Int?
    var user_id: Int?
    var message: String?
}

I called it Alamofire, but it does not come back when I do response.data.

        Alamofire.request("https://myurl.com.tr/api/tickets/\(userid)").responseJSON { (response) in

        switch response.result {
        case .success:
            if((response.result) != nil) {
                let jsonData = response.data
                print("jsonData: \(test)")
                do{
                self.allReplies = try JSONDecoder().decode([TeknikDestek].self, from: jsonData!)
                    print(self.allReplies)

                    for reply in self.allReplies {
                        print("Reply: \(reply)")
                    }
                }catch {
                    print("Error: \(error)")
                }
                self.view.dismissNavBarActivity()
            }
        case .failure(let error):
            print(error)
        }
    }

This is the error console: Error console

How can I make it work? I've spent several hours now but without success. Please help me. Many Thanks.

Answer

vadian picture vadian · Jan 19, 2018

The question is not related to Alamofire. It's only related to JSONDecoder / Decodable

You need an umbrella struct for the root object, which is a dictionary containing the data key, not an array. That's what the error message states.

struct Root : Decodable {
    let data : [TeknikDestek]
}

Then decode Root

let root = try JSONDecoder().decode(Root.self, from: jsonData!)

and get the replies with

self.allReplies = root.data.first?.replies // returns `nil` if data is empty

Note: It's highly recommended to name data structs in singular form (e.g. Reply), semantically you have a collection of singular items