JSONDecoder从字符串中的字符串提取数据?
代码11.4 SWIFT 5.0 我使用API从JSON获取字符串。这是我的API结果:JSONDecoder从字符串中的字符串提取数据?,json,swift,api,Json,Swift,Api,代码11.4 SWIFT 5.0 我使用API从JSON获取字符串。这是我的API结果: { "altimeter": { "repr": "3022", "spoken": "three zero point two two", "value": 30.22 }, "clouds": [ { "altitude": 250, "modifier": null, "repr": "FEW250", "ty
{
"altimeter": {
"repr": "3022",
"spoken": "three zero point two two",
"value": 30.22
},
"clouds": [
{
"altitude": 250,
"modifier": null,
"repr": "FEW250",
"type": "FEW"
}
],
"dewpoint": {
"repr": "25",
"spoken": "two five",
"value": 25
},
"flight_rules": "VFR",
"meta": {
"timestamp": "Tue, 04 Sep 2018 01:01:47 GMT"
},
"other": [],
"raw": "KJFK 040051Z 20007KT 10SM FEW250 26/25 A3022 RMK AO2 SLP231 T02560250",
"remarks": "RMK AO2 SLP231 T02560250",
"remarks_info": {
"dewpoint_decimal": {
"repr": "25.0",
"spoken": "two five point zero",
"value": 25
},
"temperature_decimal": {
"repr": "25.6",
"spoken": "two five point six",
"value": 25.6
}
},
"runway_visibility": [],
"sanitized": "KJFK 040051Z 20007KT 10SM FEW250 26/25 A3022 RMK AO2 SLP231 T02560250",
"station": "KJFK",
"temperature": {
"repr": "26",
"spoken": "two six",
"value": 26
},
"time": {
"dt": "Tue, 04 Sep 2018 00:51:00 GMT",
"repr": "040051Z"
},
"units": {
"altimeter": "inHg",
"altitude": "ft",
"temperature": "C",
"visibility": "sm",
"wind_speed": "kt"
},
"visibility": {
"repr": "10",
"spoken": "one zero",
"value": 10
},
"wind_direction": {
"repr": "200",
"spoken": "two zero zero",
"value": 200
},
"wind_gust": null,
"wind_speed": {
"repr": "07",
"spoken": "seven",
"value": 7
},
"wind_variable_direction": []
}
我能够提取单个字符串信息,如“原始”和“站点”,但难以提取信息中的信息。例如“时间”->“返回”
任何人都可以帮助提取时间,而不仅仅是单个字符串?您需要一个嵌套结构。试试这个:
struct AeroWX: Decodable {
struct Time: Decodable {
let dt: Date
let repr: String
}
var raw: String
var station: String
var time: Time
}
对于解码器的日期解码策略,您还需要自定义日期格式化程序。要解码,您需要执行以下操作:
let formatter = DateFormatter()
formatter.dateFormat = "E, dd MMM yyyy HH:mm:ss 'GMT'"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(formatter)
let aero = try decoder.decode(AeroWX.self, from: json)
我应该提到的最后一件事是,在json数据包的日期字段中提供实际的时区缩写是非常不传统的。您应该与您的web开发人员交谈,了解除了“GMT”之外您还可以期望哪些值。此日期格式化程序将始终使用GMT。您需要一个嵌套结构。试试这个:
struct AeroWX: Decodable {
struct Time: Decodable {
let dt: Date
let repr: String
}
var raw: String
var station: String
var time: Time
}
对于解码器的日期解码策略,您还需要自定义日期格式化程序。要解码,您需要执行以下操作:
let formatter = DateFormatter()
formatter.dateFormat = "E, dd MMM yyyy HH:mm:ss 'GMT'"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(formatter)
let aero = try decoder.decode(AeroWX.self, from: json)
我应该提到的最后一件事是,在json数据包的日期字段中提供实际的时区缩写是非常不传统的。您应该与您的web开发人员交谈,了解除了“GMT”之外您还可以期望哪些值。此日期格式化程序将始终使用GMT。请阅读。永远不要尝试要解码,
捕获错误
和打印
它。谢谢David,这是我第一次尝试的,使用了CodingKeys,但这给了我一个URLSession字符串错误,我无法修复,说它是无效的,我找不到其他格式来处理它。你知道我该如何改进我的URLSession,使它能和编码键一起工作吗?请阅读。永远不要尝试
要解码,捕获错误
和打印
它。谢谢David,这是我第一次尝试的,使用了CodingKeys,但这给了我一个URLSession字符串错误,我无法修复,说它是无效的,我找不到其他格式来处理它。你知道我该如何改进我的URLSession,使它能和编码键一起工作吗?我以前也试过类似的方法。我从JSON解码行得到一个致命错误:致命错误:“试试!”表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Double,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“time”,intValue:nil),CodingKeys(stringValue:“dt”,intValue:nil)],debugDescription:“应解码双精度,但找到字符串/数据。”,UnderingError:nil):文件致命错误:“重试!”表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Double,Swift.DecodingError.Context(CodingPath没有合适的日期解码策略会崩溃。对于初学者,我建议只解码String
。很抱歉,我没有时间运行代码。@vadian是正确的,解码器不知道如何解析日期。JSONDecoder
上有一个dateDecodingStrategy
属性,需要进行规范ified。我过一会儿会给你回电。@vadian试一试。我用自定义日期格式化程序更新了我的答案。代码现在不会抛出错误吗?就像我的常规json try!语句一样,我不会尝试获取这些值,而是通过错误返回。我需要做什么调整才能检索(retr)嵌套时间结构的一部分?我以前尝试过类似的操作。我从JSON解码行中得到了一个致命错误:致命错误:“try!”表达式意外地引发了一个错误:Swift.DecodingError.typemissmatch(Swift.Double,Swift.DecodingError.Context)(codingPath:[CodingKeys(stringValue:“time”,intValue:nil),CodingKeys(stringValue:“dt”),intValue:nil)],debugDescription:“应解码双精度,但找到了字符串/数据。”,underlineError:nil)):文件致命错误:“try!”表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Double,Swift.DecodingError.Context(CodingPath没有合适的日期解码策略会崩溃。对于初学者,我建议只解码String
。很抱歉,我没有时间运行代码。@vadian是正确的,解码器不知道如何解析日期。JSONDecoder
上有一个dateDecodingStrategy
属性,需要进行规范ified。我过一会儿会给你回电。@vadian试一试。我用自定义日期格式化程序更新了我的答案。代码现在不会抛出错误吗?就像我的常规json try!语句一样,我不会尝试获取这些值,但我会通过错误来获取这些值。我需要做什么调整才能从嵌套的时间结构中检索(retr)部分?