gpt4 book ai didi

ios - Swift-在JSON文件中搜索特定值并在同一数组中获取整个字典数组

转载 作者:行者123 更新时间:2023-12-01 21:26:38 25 4
gpt4 key购买 nike

这有点令人费解,但是我有一个包含邮政编码和城市数据的JSON文件,其格式如下:

[{
"zip_code" : 32071,
"latitude" : 30.036193,
"longitude" : -82.932228,
"city" : "O Brien",
"state" : "FL",
"county" : "Suwannee"
},
{
"zip_code" : 32072,
"latitude" : 30.36036,
"longitude" : -82.25418,
"city" : "Olustee",
"state" : "FL",
"county" : "Baker"
},
{
"zip_code" : 32073,
"latitude" : 30.119884,
"longitude" : -81.791546,
"city" : "Orange Park",
"state" : "FL",
"county" : "Clay"
},
{
"zip_code" : 32079,
"latitude" : 29.984882,
"longitude" : -81.802221,
"city" : "Penney Farms",
"state" : "FL",
"county" : "Clay"
},
{
"zip_code" : 32080,
"latitude" : "",
"longitude" : "",
"city" : "Saint Augustine",
"state" : "FL",
"county" : "Saint Johns"
},
{
"zip_code" : 46929,
"latitude" : 40.556269,
"longitude" : -86.490521,
"city" : "Flora",
"state" : "IN",
"county" : "Carroll"
}]
我想在我的应用程序中执行的操作是给它提供一个邮政编码,然后从包含该邮政编码的数组中返回数据。因此,如果我正在寻找 32071,它将在对象中返回 ziplatitudelongitude等,然后可以通过 object.cityobject.county引用它。
尝试使用 getIndex(for:)进行此操作,但是没有用。我能够成功打印解码后的数据,只是无法进行下一步工作。
编辑:这是我的解码器:
struct City: Codable {
let zipCode: Int
let latitude, longitude: Itude
let city: String
let state: State
let county: String

enum CodingKeys: String, CodingKey {
case zipCode = "zip_code"
case latitude, longitude, city, state, county
}
}

enum Itude: Codable {
case double(Double)
case string(String)

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Double.self) {
self = .double(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(Itude.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Itude"))
}

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .double(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}

enum State: String, Codable {
case aa = "AA"
case ae = "AE"
case ak = "AK"
case al = "AL"
case ap = "AP"
case ar = "AR"
case az = "AZ"
case ca = "CA"
case co = "CO"
case ct = "CT"
case dc = "DC"
case de = "DE"
case fl = "FL"
case fm = "FM"
case ga = "GA"
case gu = "GU"
case hi = "HI"
case ia = "IA"
case id = "ID"
case il = "IL"
case ks = "KS"
case ky = "KY"
case la = "LA"
case ma = "MA"
case md = "MD"
case me = "ME"
case mh = "MH"
case mi = "MI"
case mn = "MN"
case mo = "MO"
case mp = "MP"
case ms = "MS"
case mt = "MT"
case nc = "NC"
case nd = "ND"
case ne = "NE"
case nh = "NH"
case nj = "NJ"
case nm = "NM"
case nv = "NV"
case ny = "NY"
case oh = "OH"
case ok = "OK"
case or = "OR"
case pa = "PA"
case pr = "PR"
case pw = "PW"
case ri = "RI"
case sc = "SC"
case sd = "SD"
case stateAS = "AS"
case stateIN = "IN"
case tn = "TN"
case tx = "TX"
case ut = "UT"
case va = "VA"
case vi = "VI"
case vt = "VT"
case wa = "WA"
case wi = "WI"
case wv = "WV"
case wy = "WY"
}

typealias Cities = [City]

最佳答案

您可以使用first(where:)-请注意,这会产生一个Optional

let result = try JSONDecoder().decode(Cities.self, from: jsonStr)

if let object = result.first(where: { $0.zipCode == 32071 }) {
print(object.county) // prints "Suwannee"
}

如果要获得多个对象,可以使用 filter
let objects = result.filter { $0.zipCode == 32071 }

关于ios - Swift-在JSON文件中搜索特定值并在同一数组中获取整个字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63385925/

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