gpt4 book ai didi

ios - 在 Swift 4 中解码 JSON 时遇到问题

转载 作者:行者123 更新时间:2023-11-28 14:28:54 24 4
gpt4 key购买 nike

这是响应 JSON:

{
"feed": {
"title": "Top Albums",
"id": "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json",
"author": {
"name": "iTunes Store",
"uri": "http://wwww.apple.com/us/itunes/"
},
"links": [
{
"self": "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json"
},
{
"alternate": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?genreId=34&popId=82&app=music"
}
],
"copyright": "Copyright © 2018 Apple Inc. All rights reserved.",
"country": "us",
"icon": "http://itunes.apple.com/favicon.ico",
"updated": "2018-07-17T01:41:38.000-07:00",
"results": [
{
"artistName": "Drake",
"id": "1405365674",
"releaseDate": "2018-06-29",
"name": "Scorpion",
"kind": "album",
"copyright": "℗ 2018 Young Money/Cash Money Records",
"artistId": "271256",
"artistUrl": "https://itunes.apple.com/us/artist/drake/271256?app=music",
"artworkUrl100": "https://is4-ssl.mzstatic.com/image/thumb/Music125/v4/5d/9b/97/5d9b97d6-9f78-e43b-7ba7-c2c42f53a166/00602567879121.rgb.jpg/200x200bb.png",
"genres": [
{
"genreId": "18",
"name": "Hip-Hop/Rap",
"url": "https://itunes.apple.com/us/genre/id18"
},
{
"genreId": "34",
"name": "Music",
"url": "https://itunes.apple.com/us/genre/id34"
}
],
"url": "https://itunes.apple.com/us/album/scorpion/1405365674?app=music"
},
...

这是我尝试解码上述 JSON 的代码:

struct object: Decodable {
struct feed: Decodable {
let title: String?
let id: Int?


struct Author: Decodable {
let name: String?
let uri: String?
}

let author: Author?
struct Link: Decodable {
let url: String?
private enum CodingKeys: String, CodingKey {
case url = "self"
}
}
let links: [Link]?
let copyright: String?
let country: String?
let icon: String?
let updated: String?

let results: [Album]?
}
}

struct Album: Decodable {
let artistName: String?
let id: Int?
let releaseDate: String?
let name: String?
let artworkUrl100: String?
let kind: String?
let copyright: String?
let artistId: Int?
let artistUrl: String?
struct Genre: Decodable {
let genreId: Int?
let name: String?
let url: String?
}
let genres: [Genre]?
let url: String?
}

我正在尝试获取包含专辑名称、艺术家姓名和图片网址的结果。我很难理解如何构建合适的结构来使用嵌套的 json 获取数据。

当我尝试在我的完成 block 中获取数据时,我最终得到的所有内容都是 nil 值。

//Networking
let jsonString = "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json"

guard let url = URL(string: jsonString) else { return }

URLSession.shared.dataTask(with: url) { (data, response, error) in

guard let data = data else { return }

do {
let obj = try JSONDecoder().decode(object.feed.self, from: data)
print(obj)
} catch let jsonError {
print("Error with json", jsonError)
}

}.resume()

我的结果全是零:

feed(title: nil, id: nil, author: nil, links: nil, copyright: nil, country: nil, icon: nil, updated: nil, results: nil)

最佳答案

你有几个问题。

首先,最外层结构是feed键下的一个对象

你需要:

struct object: Decodable {
let feed: Feed
}

struct Feed: Decodable {
let title: String
let id: String

let author: Author
struct Link: Decodable {
let url: String?
private enum CodingKeys: String, CodingKey {
case url = "self"
}
}
let links: [Link]
let copyright: String
let country: String
let icon: String
let updated: String

let results: [Album]
}

然后您将通过以下方式对其进行解码:

do {
let obj = try JSONDecoder().decode(object.self, from: data)
print(obj)
} catch let jsonError {
print("Error with json", jsonError)
}

请注意,所有“id”都是 String 而不是 Int

关于ios - 在 Swift 4 中解码 JSON 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com