我需要在嵌套的 JSON 深处检索数据,但我在这样做时遇到了很多麻烦。可以在 https://waterservices.usgs.gov/nwis/iv/?format=json&indent=on&sites=08155200¶meterCd=00065&siteStatus=all 找到相关文件。
// MARK: - Post
struct Post: Codable {
let name, declaredType, scope: String?
let value: PostValue?
let postNil, globalScope, typeSubstituted: Bool?
enum CodingKeys: String, CodingKey {
case name, declaredType, scope, value
case postNil
case globalScope, typeSubstituted
}
}
// MARK: - PostValue
struct PostValue: Codable {
let queryInfo: QueryInfo?
let timeSeries: [TimeSery]?
}
// MARK: - QueryInfo
struct QueryInfo: Codable {
let queryURL: String?
let criteria: Criteria?
let note: [Note]?
}
// MARK: - Criteria
struct Criteria: Codable {
let locationParam, variableParam: String?
let parameter: [JSONAny]?
}
// MARK: - Note
struct Note: Codable {
let value, title: String?
}
// MARK: - TimeSery
struct TimeSery: Codable {
let sourceInfo: SourceInfo?
// let variable: Variable?
// let values: [TimeSeryValue]?
let name: String?
}
// MARK: - SourceInfo
struct SourceInfo: Codable {
let siteName: String?
// let siteCode: [SiteCode]?
// let timeZoneInfo: TimeZoneInfo?
let geoLocation: GeoLocation?
let note, siteType: [JSONAny]?
// let siteProperty: [SiteProperty]?
}
// MARK: - GeoLocation
struct GeoLocation: Codable {
let geogLocation: GeogLocation?
let localSiteXY: [JSONAny]?
}
// MARK: - GeogLocation
struct GeogLocation: Codable {
let srs: String?
let latitude, longitude: Double?
}
获取数据的代码:
URLSession.shared.dataTask(with: url) { data, _, _ in
if let data = data {
let posts = try! JSONDecoder().decode(Post.self, from: data)
if let coord = (posts.value?.timeSeries?.sourceInfo?.geoLocation?.geogLocation?.srs) {
print(coord)
}
}
}.resume()
不幸的是,这会返回错误
error: ParseJSON.playground:330:89: error: type of expression is ambiguous without more context
答案 0 :(得分:1)
timeSeries
被定义为 [TimeSery]
,这意味着它是一个数组,但您试图访问它,就好像它只是一个单一的值。由于我不确定您的意图是什么,因此很难说确切的解决方法是什么,但一种可能性是从中访问 first
值(相当于要求 [0]
,但它返回一个可选):
posts.value?.timeSeries?.first?.sourceInfo?.geoLocation?.geogLocation?.srs
顺便说一下,调试这个问题的一种方法是将表达式分解成不太复杂的部分(我从 posts.value
开始,然后添加代码直到找到问题为止(在这种情况下,{ {1}})