Swift 4 - JSON parsing with Codable protocol (nest

2019-06-07 09:00发布

问题:

I am trying to update myself and use modern and recent Swift 4 features.

That is why I am training with the Codable protocol in order to parse JSON and directly map my model object.

First of all, I did some research and self learning.

This article helped me a lot : Ultimate guide

I just need to focus on the "Com" array.

As you can notice, it contains some nested object. I named them Flash Info.

It is defined by :

  • endDate
  • text
  • image[]
  • title
  • productionDate
  • id

So here is my Codable Struct :

struct FlashInfo : Codable {

    let productionDate: String
    let endDate: String
    let text: String
    let title: String
    let id: String
}

First of all, I was trying to parse it without the array of Images, I will handle it later.

So here is my method :

func getFlashInfo(success: @escaping (Array<FlashInfo>)  -> Void) {

        var arrayFlash = [FlashInfo]()

        Alamofire.request(URL_TEST, method: .get).responseJSON { response in
            if response.value != nil {
                if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                    print("Data: \(utf8Text)")
                }

                //let decoder = JSONDecoder()
                // let flash = try! decoder.decode(FlashInfo.self, from: response.data!)
                // arrayFlash.append(flash)

                success(arrayFlash)
            } else {
                print("error getFlashInfo")
            }
        }
    }

I don't know how to handle the fact that I only need the "Com" array and how to iterate through all nested objects in order to fill my array in the callback.

I mean, will the decode protocol iterate through each objects ?

Am I clear?

EDIT: JSON as text

{"Test": [], "Toto": [], "Com": [{"endDate": "2017-06-27T08:00:00Z", "text": "John Snow is getting married", "image": ["895745-test.png", "632568-test.png"], "titre": "We need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9686"}, {"endDate": "2017-07-27T08:00:00Z", "text": "LOL TEST", "image": ["895545-test.png", "632568-test.png"], "titre": "She needs you!", "productionDate": "2017-08-02T16:16:23Z", "id": "9687"},{"endDate": "2017-06-27T08:00:00Z", "text": "iOS swift", "image": ["895775-test.png", "638568-test.png"], "titre": "They need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9688"}], "Yt": []}

回答1:

I believe the quickest way is just to define an incomplete Response type as well. For instance:

struct Response: Codable {
    let Com: [FlashInfo]
}

struct FlashInfo: Codable {
    let productionDate: String
    let endDate: String
    let text: String
    let title: String
    let id: String
    let image: [String] = [] // Ignored for now.

    enum CodingKeys: String, CodingKey {
        case productionDate, endDate, text, id
        case title = "titre" // Fix for JSON typo ;)
    }
}

and decode it like this:

let decoder = JSONDecoder()
let response = try! decoder.decode(Response.self, from: data)
print(response.Com)

This worked great with the test data you provided (just watch out for the typo in the title field):

let json = """
{"Test": [], "Toto": [], "Com": [{"endDate": "2017-06-27T08:00:00Z", "text": "John Snow is getting married", "image": ["895745-test.png", "632568-test.png"], "titre": "We need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9686"}, {"endDate": "2017-07-27T08:00:00Z", "text": "LOL TEST", "image": ["895545-test.png", "632568-test.png"], "titre": "She needs you!", "productionDate": "2017-08-02T16:16:23Z", "id": "9687"},{"endDate": "2017-06-27T08:00:00Z", "text": "iOS swift", "image": ["895775-test.png", "638568-test.png"], "titre": "They need you!", "productionDate": "2017-07-02T16:16:23Z", "id": "9688"}], "Yt": []}
"""
let data = json.data(using: .utf8)!