如何从复杂的JSON响应创建结构模型

vpfxa7rd  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(100)

我正在尝试使用swift中API的数据,json数据已经成功地传递回应用程序,但是后端的json响应非常复杂,因此为我的模型构造struct非常困难。我只能检索简单的字符串,但是如果我添加对象和数组,所有东西都停止工作

[
    {
        "type": "movie",
        "id": "ffff-ddd968-4cf0-a939-8skeu",
        "title": "Sorority Row",
        "description": "When five laldkdk",
        "categories": [],
        "genres": [
            "Mystery",
            "Horror"
        ],
        "links": {
            "amazonPrime": [
                {
                    "link": "somelink",
                    "format": "native_ios"
                },
                {
                    "link": "somelink",
                    "format": "native_ios"
                }
            ],
            "appleTvPlus": [],
            "disneyPlus": []
            "iTunes": [
                {
                    "link": "www.somelink",
                    "format": "webview_computer"
                }
            ],
            "netflix": [],
            "youTubePremium": []
        },
        "promoted": false,
        "certification": "18",
        "releaseDate": "2009-09-09",
        "runTime": 101,
        "userRating": null,
        "inUserList": false,
        "packShot": {
            "thumbnail": "imageurl"
        },
        "backdrop": {
            "thumbnail": "imageurl"
        }
    }
   ]
    
    struct Responder: Codable {
    let type: String
    let id: String
    let description: String
    let title: String
    let promoted: Bool
    let certification: String
    let firstAirDate: String
    let lastAirDate: String
    let numberEpisodes: Int
    let numberSeasons: Int
    let userRating: Int?
    let inUserList: Bool
    let thumbnail: PackShotObj
    let amazonPrime: linksObj
}

struct PackShotObj: Codable {
    let packShot: [String]
}

struct linksObj: Codable {
    let link: String
    let format: String
}

struct genres: Codable {
    let empty: String
}
mf98qq94

mf98qq941#

下面是解码你的json数据的代码,注意我的结构模型和你的结构模型之间的区别,你需要参考服务器的文档来确定哪些字段是可选的,并调整代码:

struct ContentView: View {
    @State var responders: [Responder] = []
    
    var body: some View {
        List(responders) { responder in
            Text(responder.title)
            Text(responder.description)
            Text(responder.releaseDate)
        }
        .onAppear {
            let json = """
[
    {
        "type": "movie",
        "id": "ffff-ddd968-4cf0-a939-8skeu",
        "title": "Sorority Row",
        "description": "When five laldkdk",
        "categories": [],
        "genres": [
            "Mystery",
            "Horror"
        ],
        "links": {
            "amazonPrime": [
                {
                    "link": "somelink",
                    "format": "native_ios"
                },
                {
                    "link": "somelink",
                    "format": "native_ios"
                }
            ],
            "appleTvPlus": [],
            "disneyPlus": [],
            "iTunes": [
                {
                    "link": "www.somelink",
                    "format": "webview_computer"
                }
            ],
            "netflix": [],
            "youTubePremium": []
        },
        "promoted": false,
        "certification": "18",
        "releaseDate": "2009-09-09",
        "runTime": 101,
        "userRating": null,
        "inUserList": false,
        "packShot": {
            "thumbnail": "imageurl"
        },
        "backdrop": {
            "thumbnail": "imageurl"
        }
    }
   ]
"""
            // simulated API data
            let data = json.data(using: .utf8)!
            do {
                self.responders = try JSONDecoder().decode([Responder].self, from: data)
                print("\n---> responders: \n \(responders)\n")
            } catch {
                print("\n---> decoding error: \n \(error)\n")
            }
        }
    }
}

// MARK: - Responder
struct Responder: Identifiable, Codable {
    let type, id, title, description: String
    let categories: [String]
    let genres: [String]
    let links: Links
    let promoted: Bool
    let certification, releaseDate: String
    let runTime: Int
    let userRating: Int?
    let inUserList: Bool
    let packShot, backdrop: Backdrop
}

// MARK: - Backdrop
struct Backdrop: Codable {
    let thumbnail: String
}

// MARK: - Links
struct Links: Codable {
    let amazonPrime: [Provider]
    let appleTvPlus: [Provider]
    let disneyPlus: [Provider]
    let iTunes: [Provider]
    let netflix: [Provider]
    let youTubePremium: [Provider]
}

struct Provider: Codable {
    let link, format: String
}
58wvjzkj

58wvjzkj2#

只要复制并粘贴此模型到文件,你是好的去。

struct Responder: Codable {
    let type, id, title, welcomeDescription: String
    let categories: [String]
    let genres: [String]
    let links: Links
    let promoted: Bool
    let certification, releaseDate: String
    let runTime: Int
    let userRating: Any?
    let inUserList: Bool
    let packShot, backdrop: Backdrop

    enum CodingKeys: String, CodingKey {
        case type, id, title
        case welcomeDescription = "description"
        case categories, genres, links, promoted, certification, releaseDate, runTime, userRating, inUserList, packShot, backdrop
    }
}

// MARK: - Backdrop
struct Backdrop: Codable {
    let thumbnail: String
}

// MARK: - Links
struct Links: Codable {
    let amazonPrime: [AmazonPrime]
    let appleTvPlus, disneyPlus: [String]
    let iTunes: [AmazonPrime]
    let netflix, youTubePremium: [String]
}

// MARK: - AmazonPrime
struct AmazonPrime: Codable {
    let link, format: String
}

相关问题