swift3 - How to parse JSON data from local JSON File swift 3? -


i trying print list of currencies , symbols json file have locally in project

guard let path: string = bundle.main.path(forresource: "common-currency", oftype: "json") else {return}     let url = url(fileurlwithpath: path)      {         let jsondata = try data(contentsof: url)         let json = try jsonserialization.jsonobject(with: jsondata, options: .mutablecontainers)         print(json)          guard let jsonarray = json as? [any] else { return }          currency in jsonarray {             guard let eachcurrency = currency as? [string: any] else {return}             guard let currencyname = eachcurrency["code"] else {return}             guard let currencysymbol = eachcurrency["symbol_native"] else {return}              print(currencyname)             print(currencysymbol)         }     }     catch {         print(error)     } 

this current code have, when run print(json) command gets executed, not other 2 prints. doing wrong?

the json looks this:

{  "currencies" : { "usd": {     "symbol": "$",     "name": "us dollar",     "symbol_native": "us$",     "decimal_digits": 2,     "rounding": 0,     "code": "usd",     "name_plural": "us dollars" }, "cad": {     "symbol": "ca$",     "name": "canadian dollar",     "symbol_native": "ca$",     "decimal_digits": 2,     "rounding": 0,     "code": "cad",     "name_plural": "canadian dollars" }, "eur": {     "symbol": "€",     "name": "euro",     "symbol_native": "€",     "decimal_digits": 2,     "rounding": 0,     "code": "eur",     "name_plural": "euros" }, "aed": {     "symbol": "aed",     "name": "united arab emirates dirham",     "symbol_native": "د.إ.‏",     "decimal_digits": 2,     "rounding": 0,     "code": "aed",     "name_plural": "uae dirhams" }, "afn": {     "symbol": "af",     "name": "afghan afghani",     "symbol_native": "؋",     "decimal_digits": 0,     "rounding": 0,     "code": "afn",     "name_plural": "afghan afghanis" }, 

the root object dictionary [string:any]. currencies value key currencies dictionary.

you currency information

let url = bundle.main.url(forresource: "common-currency", withextension: "json")! {     let jsondata = try data(contentsof: url)     let json = try jsonserialization.jsonobject(with: jsondata) as! [string:any]     print(json)      let currencies = json["currencies"] as! [string: [string:any]]      (key, currency) in currencies {         let currencyname = currency["name"] as! string         let currencysymbol = currency["symbol_native"] as! string          print(key) // == `code`         print(currencyname)         print(currencysymbol)     } } catch {     print(error) } 

for alphabetical order have keys, sort them , currency dictionary key.

        let currencies = json["currencies"] as! [string: [string:any]]         let currencycodes = currencies.keys.sorted()          code in currencycodes {             let currency = currencies[code]!             let currencyname = currency["name"] as! string             let currencysymbol = currency["symbol_native"] as! string              print(code)             print(currencyname)             print(currencysymbol)         } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -