嘿,我想解码此json数据,我想获取城市,但是如果删除模型中的位置,其他人都可以,但是失败了。谁能帮我这是我的代码。
"results": {
"datetime": [
{
"times": {
"Imsak": "04:07",
"Sunrise": "05:32",
"Fajr": "04:17",
"Dhuhr": "11:47",
"Asr": "15:14",
"Sunset": "18:02",
"Maghrib": "18:16",
"Isha": "19:13",
"Midnight": "23:10"
},
"date": {
"timestamp": 1544659200,
"gregorian": "2018-12-13",
"hijri": "1440-04-06"
}
}
],
"location": {
"latitude": -6.2375,
"longitude": 106.69556,
"elevation": 26,
"city": "Ciputat",
"country": "Republic of Indonesia",
"country_code": "ID",
"timezone": "Asia/Jakarta",
"local_offset": 7
}
}
这是我的模型对象,其中城市关联键中没有值,但没有值,但是奇怪的是,如果删除结构结果下的var位置,并且所有关联都可以正常工作,则只有位置是问题。
struct PrayerModel: Decodable {
var results: Results
}
struct Results: Decodable {
var datetime: [DateTime]
var location: Location
}
struct Location: Decodable {
var city: String
init(dictionary: [String: String]) {
self.city = dictionary["city"] ?? ""
}
}
struct DateTime: Decodable {
var times: Times
}
struct Times: Decodable {
var Imsak: String
var Sunrise: String
var Fajr: String
var Dhuhr: String
var Asr: String
var Sunset: String
var Maghrib: String
var Isha: String
var Midnight: String
init(dictionary: [String: String]) {
self.Imsak = dictionary["Imsak"] ?? ""
self.Sunrise = dictionary["Sunrise"] ?? ""
self.Fajr = dictionary["Fajr"] ?? ""
self.Dhuhr = dictionary["Dhuhr"] ?? ""
self.Asr = dictionary["Asr"] ?? ""
self.Sunset = dictionary["Sunset"] ?? ""
self.Maghrib = dictionary["Maghrib"] ?? ""
self.Isha = dictionary["Isha"] ?? ""
self.Midnight = dictionary["Midnight"] ?? ""
}
}
这是我的错误出现的地方,错误在哪里?我不明白
Failed to decode data: keyNotFound(CodingKeys(stringValue: "city", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), CodingKeys(stringValue: "location", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"city\", intValue: nil) (\"city\").", underlyingError: nil))
答案 0 :(得分:1)
这对我有用,不确定您为什么使用带有Codable的字典中的init
import UIKit
import PlaygroundSupport
let jsonData = """
{
"results": {
"datetime": [{
"times": {
"Imsak": "04:07",
"Sunrise": "05:32",
"Fajr": "04:17",
"Dhuhr": "11:47",
"Asr": "15:14",
"Sunset": "18:02",
"Maghrib": "18:16",
"Isha": "19:13",
"Midnight": "23:10"
},
"date": {
"timestamp": 1544659200,
"gregorian": "2018-12-13",
"hijri": "1440-04-06"
}
}],
"location": {
"latitude": -6.2375,
"longitude": 106.69556,
"elevation": 26,
"city": "Ciputat",
"country": "Republic of Indonesia",
"country_code": "ID",
"timezone": "Asia/Jakarta",
"local_offset": 7
}
}
}
""".data(using: .utf8)!
struct PrayerModel: Decodable {
var results: Results
}
struct Results: Decodable {
var datetime: [DateTime]
var location: Location
}
struct Location: Decodable {
var city: String
}
struct DateTime: Decodable {
var times: Times
}
struct Times: Decodable {
var Imsak: String
var Sunrise: String
var Fajr: String
var Dhuhr: String
var Asr: String
var Sunset: String
var Maghrib: String
var Isha: String
var Midnight: String
}
do {
let result = try JSONDecoder().decode(PrayerModel.self, from: jsonData)
print(result)
print("city: \(result.results.location.city)")
} catch {
print(error)
}
输出:
PrayerModel(结果:__lldb_expr_53.Results(日期时间:[__lldb_expr_53.DateTime(时间:__lldb_expr_53.Times(Imsak:“ 04:07”,Sunrise:“ 05:32”,Fajr:“ 04:17”,Dhuhr: “ 11:47”,Asr:“ 15:14”,日落:“ 18:02”,Maghrib:“ 18:16”,Isha:“ 19:13”,午夜:“ 23:10”)))],位置:__lldb_expr_53.Location(city:“ Ciputat”)))
城市:Ciputat