如何从本地JSON文件swift 3解析JSON数据?

时间:2017-08-18 08:28:05

标签: json swift3

我正在尝试从项目中本地的JSON文件中打印货币及其符号列表

guard let path: String = Bundle.main.path(forResource: "Common-Currency", ofType: "json") else {return}
    let url = URL(fileURLWithPath: path)

    do {
        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 }

        for 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)
    }

这是我当前的代码,但是当我运行它时,只执行print(json)命令,而不执行其他2次打印。我究竟做错了什么?

json看起来像这样:

{

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

1 个答案:

答案 0 :(得分:1)

根对象是字典[String:Any]。货币是键currencies的值,它也是字典。

您可以使用

获取货币信息
let url = Bundle.main.url(forResource: "Common-Currency", withExtension: "json")!
do {
    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]]

    for (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)
}

对于字母顺序,您必须获取密钥,对其进行排序并按键获取货币字典。

        let currencies = json["Currencies"] as! [String: [String:Any]]
        let currencyCodes = currencies.keys.sorted()

        for 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)
        }