D
D
Dima Grib2021-04-11 16:50:30
JSON
Dima Grib, 2021-04-11 16:50:30

Parsing Json, with nested reference to another Json?

Greetings

Tell me how to parse json, in which there is a link to another json, in one request. Since I fill from this Json CollectionView, and if I make a second request, then I need to make crutches with the correct display of pictures to cells. The situation is as follows

1. I have a file structure

struct Headline: Codable {
    var articles: [Article]
}


struct Article: Codable {
    let id: Int?
    let date, dateGmt: String?
    let links: Links?

    enum CodingKeys: String, CodingKey {
        case id, date
        case dateGmt = "date_gmt"
        case links = "_links"
    }
}

struct Links: Codable {
    let linksSelf, collection, about: [About]? // []
    let wpFeaturedmedia: [Author]?

    enum CodingKeys: String, CodingKey {
        case linksSelf = "self"
        case collection, about, author, replies
        case wpFeaturedmedia = "wp:featuredmedia"
    }
}

struct Author: Codable {
    let embeddable: Bool?
    let href: String?

    enum CodingKeys: String, CodingKey {
        case href
        case embeddable
    }
}


The Author structure contains href, in which there is a link to another JSON, from which I need to get a link

. I get all this as standard

static func getArticles(url: URL?, completion: @escaping ([Article]?) -> Void) {
        url?.get(completion: { (result: Result<[Article], ApiError>) in
            switch result {
            case .success(let headline):
                print("\n News Api extension getArticles headline \(String(describing: headline.count))")
                completion(headline)
            case .failure(_):
                print("\n News Api getArticles failure")
                completion(nil)
            }
        })
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question