gpt4 book ai didi

swift - 从 URL 解析 JSON 最终出现错误 - Swift 5

转载 作者:行者123 更新时间:2023-12-04 10:05:34 25 4
gpt4 key购买 nike

我正在尝试在 swift 中编写一个函数,该函数从 URL JSON 获取数据,并将其分配给 swift 中的变量。

这是函数:

func getBikeData(){
guard let url = URL(string: "https://api.citybik.es//v2/networks/baksi-bisim") else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
print(jsonResponse) //Response result


do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
//Decode JSON Response Data

let model = try decoder.decode(Station.self,
from: dataResponse)
print(model.freeBikes) //Output - 1221

} catch let parsingError {
print("Error", parsingError)
}
} catch let parsingError {
print("Error", parsingError)

}
}
task.resume()

}

这是我添加的结构,以及我需要的数据:
    // MARK: - Station
struct Station: Codable {
let emptySlots: Int
let extra: Extra
let freeBikes: Int
let id: String
let latitude, longitude: Double
let name, timestamp: String

enum CodingKeys: String, CodingKey {
case emptySlots
case extra
case freeBikes
case id, latitude, longitude, name, timestamp
}
}

// MARK: - Extra
struct Extra: Codable {
let slots: Int
let status: Status
let uid: String
}

enum Status: String, Codable {
case active = "Active"
}

这是我一直收到的错误:
Error keyNotFound(CodingKeys(stringValue: "emptySlots", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"emptySlots\", intValue: nil) (\"emptySlots\").", underlyingError: nil))

这是我第一次使用 JSON 文件,也许我遗漏了一些非常简单的东西。请帮忙。

最佳答案

以下是如何将所有 json 数据解码为 swift 结构:

import Foundation


struct Stations: Codable {
let company: [String]
let href: String
let id: String
let location: LocationJson
let name: String
let stations: [Station]

}


struct Station: Codable {
let empty_slots: Int
let extra: Extra
let free_bikes: Int
let id: String
let latitude: Double
let longitude: Double
let name: String
let timestamp: String

}

struct ResponseJSON: Codable {
let network: Stations
}

struct LocationJson: Codable {
let city: String
let country: String
let latitude: Double
let longitude: Double
}


struct Extra: Codable {
let slots: Int
let status: String
let uid: String
}


func getBikeData(){
guard let url = URL(
string: "https://api.citybik.es//v2/networks/baksi-bisim"
) else { return }

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data, error == nil else {
print(error?.localizedDescription ?? "Response Error")
return
}
do {
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
print(jsonResponse) //Response result


do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
//Decode JSON Response Data

let model = try decoder.decode(
ResponseJSON.self, from: dataResponse
)
print(model) //Output - 1221

} catch let parsingError {
print("Error", parsingError)
}

} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()

}

getBikeData()

关于swift - 从 URL 解析 JSON 最终出现错误 - Swift 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61604665/

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