json - Type "Any" has no subscript members in Swift 3 during pulling array of data from server -


this question has answer here:

i'm trying update project swift 3.0 , codes pulling data server give me error in following picture.

enter image description here

i tried lot of solutions available here no useful result problem in case ?

 {         let json = try jsonserialization.jsonobject(with: data, options: .allowfragments)           if let countries = json["countries"] as? [string: anyobject] {             country in countries {                 if let couname = country["countryname"] as? [anyobject] {                     country_names.append(couname)                 }                  if let coucode = country["code"] as? [anyobject] {                     country_codes.append(coucode)                 }              }         }     } catch {         print("error serializing json: \(error)")     } 

try casting json [string: any] before using it.

also seem have error here: if let couname = country["countryname"] as? [anyobject]

you should cast array of [string: anyobject]: [[string: anyobject]]

the adjusted code like:

do {     let json = try jsonserialization.jsonobject(with: data, options: .allowfragments) as! [string: any]      if let countries = json["countries"] as? [[string: anyobject]] {         country in countries {             if let couname = country["countryname"] as? [anyobject] {                 country_names.append(couname)             }              if let coucode = country["code"] as? [anyobject] {                 country_codes.append(coucode)             }          }     } } catch {     print("error serializing json: \(error)") } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -