Я использую Swift 5, чтобы создать приложение iOS, которое взаимодействует с API на основе локали пользователя. В настоящее время у меня возникают проблемы только с разбором одного JSONObject, а не с разбором / печатью результатов всех JSONObject в массиве. Другими словами, я хотел бы получить пару ключ / значение на основе другой пары ключ / значение. Конечная точка, которую я использую: https://covid19api.herokuapp.com/confirmed. Я пытаюсь получить код страны пользователя с помощью этого фрагмента (который работает, проблем там нет):
import Foundation
let locale = NSLocale.current
func requestLocale() -> String {
return(locale.regionCode!)
}
И мне нужно выполнить поиск в JSONObject, который содержит этот код страны, для элемента с именем latest
. Вот мой код для запроса API (возвращаются некоторые данные, которые я поместил под кодом):
import Foundation
struct WorldWide: Decodable {
let latest: Int
let locations: [Country]
}
struct Country: Decodable {
let country_code: String = "\(requestLocale())"
let latest: Int
}
func APIRequest() {
guard let url = URL(string: "https://covid19api.herokuapp.com/confirmed") else {fatalError("Could not get data from the API")}
let session = URLSession.shared
session.dataTask(with: url) {(data, response, error) in
guard let data = data else {return}
do {
let decoded = try JSONDecoder().decode(WorldWide.self, from: data)
print(decoded)
} catch {print(error)}
}.resume()
}
Имейте в виду, что первый latest
в WorldWide
находится на верхнем уровне, тогда как JSONObjects находятся в массиве с именем locations
, и каждый JSONObject содержит latitude
, longitude
, country
, country_code
, history
и latest
(я не использую camelCase, потому что набор данных не ). Все, что мне нужно для доступа, это country_code
, который у меня есть из языкового стандарта пользователя, и latest
, который мне нужно получить ТОЛЬКО из JSONObject, который содержит country_code
.
Запуск приведенного выше кода даст мне следующее:
WorldWide(latest: 1426096, locations: [Dark.Country(country_code: "US", latest: 423), Dark.Country(country_code: "US", latest: 383), Dark.Country(country_code: "US", latest: 1468), Dark.Country(country_code: "US", latest: 545), Dark.Country(country_code: "US", latest: 17), Dark.Country(country_code: "US", latest: 19), Dark.Country(country_code: "US", latest: 1628), Dark.Country(country_code: "US", latest: 853), Dark.Country(country_code: "US", latest: 96), Dark.Country(country_code: "US", latest: 2686), Dark.Country(country_code: "US", latest: 28), Dark.Country(country_code: "US", latest: 934), Dark.Country(country_code: "US", latest: 411), Dark.Country(country_code: "US", latest: 89), Dark.Country(country_code: "US", latest: 1191), Dark.Country(country_code: "US", latest: 460), Dark.Country(country_code: "US", latest: 12639), Dark.Country(country_code: "US", latest: 717), Dark.Country(country_code: "US", latest: 33), Dark.Country(country_code: "US", latest: 811), Dark.Country(country_code: "US", latest: 164), Dark.Country(country_code: "US", latest: 63), Dark.Country(country_code: "US", latest: 861), Dark.Country(country_code: "US", latest: 22194), Dark.Country(country_code: "US", latest: 26), Dark.Country(country_code: "US", latest: 5), Dark.Country(country_code: "US", latest: 194), Dark.Country(country_code: "US", latest: 764), Dark.Country(country_code: "US", latest: 14034), Dark.Country(country_code: "US", latest: 135), Dark.Country(country_code: "US", latest: 577), Dark.Country(country_code: "US", latest: 384), Dark.Country(country_code: "US", latest: 7), Dark.Country(country_code: "US", latest: 115), Dark.Country(country_code: "US", latest: 658), Dark.Country(country_code: "US", latest: 1373), Dark.Country(country_code: "US", latest: 1266), Dark.Country(country_code: "US", latest: 13), Dark.Country(country_code: "US", latest: 217), Dark.Country(country_code: "US", latest: 105), Dark.Country(country_code: "US", latest: 228), Dark.Country(country_code: "US", latest: 310), Dark.Country(country_code: "US", latest: 4726), Dark.Country(country_code: "US", latest: 22), Dark.Country(country_code: "US", latest: 9340), Dark.Country(country_code: "US", latest: 260), Dark.Country(country_code: "US", latest: 8), Dark.Country(country_code: "US", latest: 10), Dark.Country(country_code: "US", latest: 5116), Dark.Country(country_code: "US", latest: 990), Dark.Country(country_code: "US", latest: 587), Dark.Country(country_code: "US", latest: 579), Dark.Country(country_code: "US", latest: 351), Dark.Country(country_code: "US", latest: 139), Dark.Country(country_code: "US", latest: 1533), Dark.Country(country_code: "US", latest: 254), Dark.Country(country_code: "US", latest: 146), Dark.Country(country_code: "US", latest: 168), Dark.Country(country_code: "US", latest: 327), Dark.Country(country_code: "US", latest: 544), Dark.Country(country_code: "US", latest: 1276), Dark.Country(country_code: "US", latest: 935), Dark.Country(country_code: "US", latest: 67803), Dark.Country(country_code: "US", latest: 1019), Dark.Country(country_code: "US", latest: 121), Dark.Country(country_code: "US", latest: 651), Dark.Country(country_code: "US", latest: 937), Dark.Country(country_code: "US", latest: 98), Dark.Country(country_code: "US", latest: 144), Dark.Country(country_code: "US", latest: 44), Dark.Country(country_code: "US", latest: 75), Dark.Country(country_code: "US", latest: 18), Dark.Country(country_code: "US", latest: 256), Dark.Country(country_code: "US", latest: 781), Dark.Country(country_code: "US", latest: 538), Dark.Country(country_code: "US", latest: 138), Dark.Country(country_code: "US", latest: 560), Dark.Country(country_code: "US", latest: 180), Dark.Country(country_code: "US", latest: 1), Dark.Country(country_code: "US", latest: 76), Dark.Country(country_code: "US", latest: 184), Dark.Country(country_code: "US", latest: 1265), Dark.Country(country_code: "US", latest: 1780), Dark.Country(country_code: "US", latest: 45), Dark.Country(country_code: "US", latest: 180), Dark.Country(country_code: "US", latest: 483), Dark.Country(country_code: "US", latest: 349), Dark.Country(country_code: "US", latest: 1282), Dark.Country(country_code: "US", latest: 712), Dark.Country(country_code: "US", latest: 396), Dark.Country(country_code: "US", latest: 494), Dark.Country(country_code: "US", latest: 5017), Dark.Country(country_code: "US", latest: 184), Dark.Country(country_code: "US", latest: 11), Dark.Country(country_code: "US", latest: 5071), Dark.Country(country_code: "US", latest: 90), Dark.Country(country_code: "US", latest: 1956), Dark.Country(country_code: "US", latest: 3747), Dark.Country(country_code: "US", latest: 1450), Dark.Country(country_code: "US", latest: 78), Dark.Country(country_code: "US", latest: 16), Dark.Country(country_code: "US", latest: 31), Dark.Country(country_code: "US", latest: 1149), Dark.Country(country_code: "US", latest: 10), Dark.Country(country_code: "US", latest: 52), Dark.Country(country_code: "US", latest: 15), Dark.Country(country_code: "US", latest: 2308), Dark.Country(country_code: "US", latest: 72), Dark.Country(country_code: "US", latest: 47), Dark.Country(country_code: "US", latest: 139), Dark.Country(country_code: "US", latest: 171), Dark.Country(country_code: "US", latest: 18), Dark.Country(country_code: "US", latest: 358), Dark.Country(country_code: "US", latest: 6), Dark.Country(country_code: "US", latest: 32), Dark.Country(country_code: "US", latest: 152), Dark.Country(country_code: "US", latest: 109069), Dark.Country(country_code: "US", latest: 30), Dark.Country(country_code: "US", latest: 4), Dark.Country(country_code: "US", latest: 196), Dark.Country(country_code: "US", latest: 107663), Dark.Country(country_code: "US", latest: 287), Dark.Country(country_code: "US", latest: 1832), Dark.Country(country_code: "US", latest: 77), Dark.Country(country_code: "US", latest: 144), Dark.Country(country_code: "US", latest: 33), Dark.Country(country_code: "US", latest: 25), Dark.Country(country_code: "US", latest: 7), Dark.Country(country_code: "US", latest: 305), Dark.Country(country_code: "US", latest: 817), Dark.Country(country_code: "US", latest: 1586), Dark.Country(country_code: "US", latest: 5311), Dark.Country(country_code: "US", latest: 2738), Dark.Country(country_code: "US", latest: 62589), Dark.Country(country_code: "US", latest: 1122), Dark.Country(country_code: "US", latest: 5709), Dark.Country(country_code: "US", latest: 9248), Dark.Country(country_code: "US", latest: 135586), Dark.Country(country_code: "US", latest: 63), Dark.Country(country_code: "US", latest: 3906), Dark.Country(country_code: "US", latest: 353), Dark.Country(country_code: "US", latest: 697), Dark.Country(country_code: "US", latest: 172), Dark.Country(country_code: "US", latest: 10331), Dark.Country(country_code: "US", latest: 743), Dark.Country(country_code: "US", latest: 228), Dark.Country(country_code: "US", latest: 548), Dark.Country(country_code: "US", latest: 548), Dark.Country(country_code: "US", latest: 14), Dark.Country(country_code: "US", latest: 78), Dark.Country(country_code: "US", latest: 880), Dark.Country(country_code: "US", latest: 2970), Dark.Country(country_code: "US", latest: 88), Dark.Country(country_code: "US", latest: 3963), Dark.Country(country_code: "US", latest: 19), Dark.Country(country_code: "US", latest: 293), Dark.Country(country_code: "US", latest: 6), Dark.Country(country_code: "US", latest: 268), Dark.Country(country_code: "US", latest: 2439), Dark.Country(country_code: "US", latest: 1056), Dark.Country(country_code: "US", latest: 79), Dark.Country(country_code: "US", latest: 15), Dark.Country(country_code: "US", latest: 241), Dark.Country(country_code: "US", latest: 1184), Dark.Country(country_code: "US", latest: 16), Dark.Country(country_code: "US", latest: 9), Dark.Country(country_code: "US", latest: 74), Dark.Country(country_code: "US", latest: 13), Dark.Country(country_code: "US", latest: 40), Dark.Country(country_code: "US", latest: 19580), Dark.Country(country_code: "US", latest: 1160), Dark.Country(country_code: "US", latest: 6), Dark.Country(country_code: "US", latest: 278), Dark.Country(country_code: "US", latest: 254), Dark.Country(country_code: "US", latest: 599), Dark.Country(country_code: "US", latest: 6086), Dark.Country(country_code: "US", latest: 371), Dark.Country(country_code: "US", latest: 4035), Dark.Country(country_code: "US", latest: 2100), Dark.Country(country_code: "US", latest: 2), Dark.Country(country_code: "US", latest: 115), Dark.Country(country_code: "US", latest: 2954), Dark.Country(country_code: "US", latest: 3764), Dark.Country(country_code: "US", latest: 4848), Dark.Country(country_code: "US", latest: 12442), Dark.Country(country_code: "US", latest: 2057), Dark.Country(country_code: "US", latest: 4417), Dark.Country(country_code: "US", latest: 7497), Dark.Country(country_code: "US", latest: 105), Dark.Country(country_code: "US", latest: 14), Dark.Country(country_code: "US", latest: 8), Dark.Country(country_code: "US", latest: 279), Dark.Country(country_code: "US", latest: 2795), Dark.Country(country_code: "US", latest: 237), Dark.Country(country_code: "US", latest: 2447), Dark.Country(country_code: "US", latest: 11), Dark.Country(country_code: "US", latest: 1481), Dark.Country(country_code: "US", latest: 581), Dark.Country(country_code: "US", latest: 1059), Dark.Country(country_code: "US", latest: 8), Dark.Country(country_code: "US", latest: 1749), Dark.Country(country_code: "US", latest: 141942), Dark.Country(country_code: "US", latest: 185), Dark.Country(country_code: "US", latest: 14), Dark.Country(country_code: "US", latest: 10), Dark.Country(country_code: "US", latest: 7693), Dark.Country(country_code: "US", latest: 22253), Dark.Country(country_code: "US", latest: 376), Dark.Country(country_code: "US", latest: 24), Dark.Country(country_code: "US", latest: 2258), Dark.Country(country_code: "US", latest: 65), Dark.Country(country_code: "US", latest: 107), Dark.Country(country_code: "US", latest: 623), Dark.Country(country_code: "US", latest: 34109), Dark.Country(country_code: "US", latest: 52), Dark.Country(country_code: "US", latest: 1462), Dark.Country(country_code: "US", latest: 2359), Dark.Country(country_code: "US", latest: 39), Dark.Country(country_code: "US", latest: 45), Dark.Country(country_code: "US", latest: 335), Dark.Country(country_code: "US", latest: 113), Dark.Country(country_code: "US", latest: 150), Dark.Country(country_code: "US", latest: 9), Dark.Country(country_code: "US", latest: 55242), Dark.Country(country_code: "US", latest: 424), Dark.Country(country_code: "US", latest: 396223), Dark.Country(country_code: "US", latest: 520), Dark.Country(country_code: "US", latest: 165), Dark.Country(country_code: "US", latest: 249), Dark.Country(country_code: "US", latest: 39), Dark.Country(country_code: "US", latest: 11), Dark.Country(country_code: "US", latest: 0), Dark.Country(country_code: "US", latest: 15), Dark.Country(country_code: "US", latest: 12), Dark.Country(country_code: "US", latest: 10), Dark.Country(country_code: "US", latest: 19), Dark.Country(country_code: "US", latest: 1), Dark.Country(country_code: "US", latest: 7), Dark.Country(country_code: "US", latest: 0), Dark.Country(country_code: "US", latest: 14), Dark.Country(country_code: "US", latest: 20), Dark.Country(country_code: "US", latest: 261), Dark.Country(country_code: "US", latest: 33), Dark.Country(country_code: "US", latest: 56), Dark.Country(country_code: "US", latest: 11), Dark.Country(country_code: "US", latest: 5), Dark.Country(country_code: "US", latest: 7), Dark.Country(country_code: "US", latest: 170), Dark.Country(country_code: "US", latest: 22), Dark.Country(country_code: "US", latest: 3), Dark.Country(country_code: "US", latest: 3), Dark.Country(country_code: "US", latest: 8), Dark.Country(country_code: "US", latest: 9), Dark.Country(country_code: "US", latest: 6), Dark.Country(country_code: "US", latest: 3), Dark.Country(country_code: "US", latest: 6), Dark.Country(country_code: "US", latest: 2), Dark.Country(country_code: "US", latest: 8), Dark.Country(country_code: "US", latest: 2), Dark.Country(country_code: "US", latest: 1), Dark.Country(country_code: "US", latest: 2), Dark.Country(country_code: "US", latest: 4), Dark.Country(country_code: "US", latest: 4)])
Я оглянулся назад на набор данных, и здесь я получаю программу, печатающую каждый экземпляр latest
и просто сопрягая его с country_code
, который я предоставил. В основном мне нужно сделать
if country_code == requestLocale() {
print(latest)
}
Но это не совсем то, что сработает, это просто теория. Если бы кто-то мог провести меня через это, это было бы здорово!