简体   繁体   English

如何在 swift 中循环遍历嵌套的 json 对象?

[英]How to loop through nested json objects in swift?

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"
               ]
            }
         ]
      }
   ]
}

I have got the answer for the following question...the problem was there multiple values for the key "long_name" for the give JSON objects..the solution for this is to loop through the "address_components" and look for the unique values for the "type" key in the JSON object..for Example in this case the key "long_name " had two values "thane" and "400604" but the unique key is the types that can be used to differentiate between those我得到了以下问题的答案......问题是给定 JSON 对象的键“long_name”有多个值......解决方案是遍历“address_components”并寻找唯一值JSON 对象中的“type”键。例如,在这种情况下,键“long_name”有两个值“thane”和“400604”,但唯一键是可用于区分这些值的类型

This is the syntax for the folloing problem!!这是以下问题的语法!

func parseJson(Json:Data)  
      {

        let decoder = JSONDecoder()

        do{
            let decodedData = try decoder.decode(MapModel.self, from: Json)

            for item in decodedData.results[0].address_components{
                if item.types[0] == "postal_code"{
                    print(item.long_name)
                }
            }
        }

        catch{

            print(error)
            return 

        }
    }

What do you mean with unable to loop through ?. 无法循环是什么意思? What is the error? 有什么错误? Is it at compile time or run time?. 是在编译时还是在运行时? As far as I can imagine, it could be because of the long_name type, it is not an Int, it's a String. 据我所能想象的,可能是因为long_name类型,它不是Int,而是字符串。

And as a comment, try to use Apple coding conventions like camel case naming. 作为注释,请尝试使用Apple编码约定,例如驼峰式命名。 And search for coding Keys to make your code more readable and attached to those conventions : 并搜索编码键,以使您的代码更具可读性并遵守这些约定:

struct AddressComponents : Decodable {
       let longName : String

       enum codingKeys : String, CodingKey {
            case longName = "long_name"
       }
}

Try 尝试

for item in decodedData.results {
    for inner in item.address_components {
         print(inner.long_name)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM