json - Type "Any" has no subscript members in Swift 3 during pulling array of data from server -
this question has answer here:
- correctly parsing json in swift 3 4 answers
i'm trying update project swift 3.0 , codes pulling data server give me error in following picture.
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
Post a Comment