How to decode a JSON property with different types? [duplicate]


November 2018


146 time


I have a JSON

"tvShow": {
    "id": 5348,
    "name": "Supernatural",
    "permalink": "supernatural",
    "url": "",
    "description": "Supernatural is an American fantasy horror television series created by Eric Kripke. It was first broadcast on September 13, 2005, on The WB and subsequently became part of successor The CW's lineup. Starring Jared Padalecki as Sam Winchester and Jensen Ackles as Dean Winchester, the series follows the two brothers as they hunt demons, ghosts, monsters, and other supernatural beings in the world. The series is produced by Warner Bros. Television, in association with Wonderland Sound and Vision. Along with Kripke, executive producers have been McG, Robert Singer, Phil Sgriccia, Sera Gamble, Jeremy Carver, John Shiban, Ben Edlund and Adam Glass. Former executive producer and director Kim Manners died of lung cancer during production of the fourth season.<br>The series is filmed in Vancouver, British Columbia and surrounding areas and was in development for nearly ten years, as creator Kripke spent several years unsuccessfully pitching it. The pilot was viewed by an estimated 5.69 million viewers, and the ratings of the first four episodes prompted The WB to pick up the series for a full season. Originally, Kripke planned the series for three seasons but later expanded it to five. The fifth season concluded the series' main storyline, and Kripke departed the series as showrunner. The series has continued on for several more seasons with Sera Gamble and Jeremy Carver assuming the role of showrunner.",
    "description_source": "",
    "start_date": "2005-09-13",
    "end_date": null,
    "country": "US",
    "status": "Running",
    "runtime": 60,
    "network": "The CW",
    "youtube_link": "6ZlnmAWL59I",
    "image_path": "",
    "image_thumbnail_path": "",
    "rating": "9.6747",
    "rating_count": "249",
    "countdown": null

The value of rating in different serials is Int ("rating": 0) or String ("rating": "9.6747").

I am parsing JSON with Codable/Decodable protocols:

struct DetailModel : Decodable {

var id : Int?
var name : String?
var permalink : String?
var url : String?
var description : String
var description_source : String?
var start_date : String?
var end_date : String?
var country : String?
var status : String?
var runtime : Int?
var network : String?
var youtube_link : String?
var image_path : String?
var image_thumbnail_path : String?
var rating: String
var rating_count : String?
var countdown : String?
var genres : [String]?
var pictures : [String]?
var episodes : [EpisodesModel]?

If rating == String, my code work and I have all the variables from JSON, but if rating == Int, all is nil. What should I do to parse all the types of variable rating at once Int and String?

My decodable function:

    func searchSerialDetail(id: Int, completion: @escaping (DetailModel?) -> Void){

    let parameters: [String: Any] = ["q": id]

    Alamofire.request(DetailNetworkLayer.url, method: .get, parameters: parameters).response { (jsonResponse) in

        if let jsonValue = {
            let jsonDecoder = JSONDecoder()
                let detail = try? jsonDecoder.decode(DetailModel.self, from: jsonValue)

Thank you.

1 answers


Вы должны реализовать свои собственные func encode(to encoder: Encoder) throwsи init(from decoder: Decoder) throwsкоторые оба свойства Codableпротокола. Затем измените ratingпеременную в перечислении

Какой будет выглядеть следующим образом:

enum Rating: Codable {
    case int(Int)
    case string(String)

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .int(let v): try container.encode(v)
        case .string(let v): try container.encode(v)

    init(from decoder: Decoder) throws {
        let value = try decoder.singleValueContainer()

        if let v = try? value.decode(Int.self) {
            self = .int(v)
        } else if let v = try? value.decode(String.self) {
            self = .string(v)

        throw Rating.ParseError.notRecognizedType(value)

    enum ParseError: Error {
        case notRecognizedType(Any)

Тогда на свой контекстуальный DetailModelпросто изменить rating: Stringкrating: Rating

Это работает, я проверил с этими строками JSON.

// int rating
    "rating": 200,
    "bro": "Success"

// string rating
    "rating": "200",
    "bro": "Success"

Изменить : Я нашел лучше swiftier способ реализации init(from decoder: Decoder) throws, который производит лучшее сообщение об ошибке, с помощью этого теперь вы можете опустить ParseErrorперечисление.

init(from decoder: Decoder) throws {
    let value = try decoder.singleValueContainer()
    do {
        self = .int(try value.decode(Int.self))
    } catch DecodingError.typeMismatch {
        self = .string(try value.decode(String.self))