I have been trying to retrieve Postal_code from the following google API but unable to loop through the JSON array
(我一直在尝试从以下Google API检索Postal_code,但是无法遍历JSON数组)
This is the function for parsing JSON
(这是解析JSON的功能)
func parseJson(Json:Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(MapModel.self, from: Json)
let postal_code = decodedData.results[0].address_components[0].long_name
print(postal_code)
} catch {
print(error)
return
}
}
this is the model:
(这是模型:)
struct MapModel: Decodable {
let results : [Result]
let status: String
let plus_code : compoundCode
}
struct compoundCode: Decodable {
let compound_code: String
}
struct Result: Decodable {
let address_components:[address_components]
}
struct address_components: Decodable {
let long_name : Int
}
This the JSON through API
(通过API的JSON)
{
"plus_code":{
"compound_code":"5WXX+7J Thane, Maharashtra, India",
"global_code":"7JFJ5WXX+7J"
},
"results":[
{
"address_components":[
{
"long_name":"400604",
"short_name":"400604",
"types":[
"postal_code"
]
},
{
"long_name":"Thane",
"short_name":"Thane",
"types":[
"administrative_area_level_2",
"political"
]
}
]
}
]
}
ask by Rajat Kotian translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…