我想用Json学习Swift UI。我想使用一个公共API来学习它。不幸的是,它总是显示错误:无法读取数据,因为格式不正确。
API文档https://www.abgeordnetenwatch.de/api/entitaeten/politician API数据https://www.abgeordnetenwatch.de/api/v2/politicians
代码如下
import SwiftUI
struct ContentView: View {
@State private var politicians: [Politician] = []
@State private var selectedPolitician: Politician? = nil
var body: some View {
NavigationView {
List(politicians, id: \.id) { politician in
Button(action: {
self.selectedPolitician = politician
}) {
Text("\(politician.firstName) \(politician.lastName)")
}
}
.navigationTitle("Politicians")
.onAppear {
self.loadPoliticians()
}
if let politician = selectedPolitician {
PoliticianDetailView(politician: politician)
}
}
}
private func loadPoliticians() {
let url = URL(string: "https://www.abgeordnetenwatch.de/api/v2/politicians")!
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let decoder = JSONDecoder()
let politicians = try decoder.decode([Politician].self, from: data)
DispatchQueue.main.async {
self.politicians = politicians
}
} catch {
print(error.localizedDescription)
}
}
}.resume()
}
}
struct PoliticianDetailView: View {
let politician: Politician
var body: some View {
VStack {
Text("\(politician.firstName) \(politician.lastName)")
.font(.title)
Text("ID: \(politician.id)")
Text("Sex: \(politician.sex)")
}
.navigationBarTitleDisplayMode(.inline)
}
}
struct Politician: Codable, Identifiable {
let id: Int
let firstName: String
let lastName: String
let sex: String
enum CodingKeys: String, CodingKey {
case id
case firstName = "first_name"
case lastName = "last_name"
case sex
}
}
1条答案
按热度按时间6fe3ivhb1#
你需要有一组结构体来镜像你从服务器获得的JSON数据。所以你得到的错误是因为你没有把JSON数据解码成正确的结构模型。
将json数据复制并粘贴到https://app.quicktype.io/后,您需要做更多的工作来调整生成的代码以满足您的目的。
以下代码解码JSON数据并将其放入
[Politician]
数组中。该代码还包含较新的NavigationStack
,而不是已弃用的NaviagtionView
。对我来说很好。