无法解码API中的JSON数据

时间:2017-12-26 16:26:28

标签: json swift api utf-8

我正在尝试使用从API获取的数据更新标签,以便在应用中打印比特币价格和百分比变化,但我无法弄清楚如何正确解码JSON。

BitcoinInfo.swift:

import Foundation
import UIKit

struct Bitcoin: Codable {
    let percentChange1h: String
    let priceEUR: String
    private enum CodingKeys: String, CodingKey {
        case percentChange1h = "percent_change_1h", priceEUR = "price_eur"
    }
}

extension Bitcoin {
    var priceEURdecimal: Decimal {
        return Decimal(string: priceEUR) ?? 0
    }
    var priceEURcurrency: String {
        Formatter.currency.locale = Locale(identifier: "fr_FR")
        return Formatter.currency.string(for: priceEURdecimal) ?? ""
    }
}

ViewController.swift:

import Foundation
import UIKit

class ViewController: UIViewController {

    let bitcoinInfoController = BitcoinInfoController()

    @IBOutlet weak var bitcoinPriceLabel: UILabel!
    @IBOutlet weak var bitcoinPercentageChangeLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        bitcoinPriceLabel.text = ""
        bitcoinPercentageChangeLabel.text = ""

        fetchBitcoinInfo { bitcoin, error in
            guard let bitcoin = bitcoin else {
                print(error!);
                return
            }
            self.updateUI(with: bitcoin)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    func updateUI(with bitcoinInfo: Bitcoin) {
        DispatchQueue.main.async {
            self.bitcoinPriceLabel.text = bitcoinInfo.priceEURcurrency
            self.bitcoinPercentageChangeLabel.text = String(format: "%.2f%%", Double(bitcoinInfo.percentChange1h) ?? 0)
        }
    }

    func fetchBitcoinInfo(completion: @escaping (Bitcoin?, Error?) -> Void) {
        let baseURL = URL(string: "https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR")!

        let url = baseURL

        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard let data = data else { return }
            do {
                if let bitcoinEUR = try JSONDecoder().decode([Bitcoin].self, from: data).first {
                    print(bitcoinEUR)
                    print(bitcoinEUR.priceEUR)
                    print(bitcoinEUR.priceEURdecimal)
                    print(bitcoinEUR.priceEURcurrency)
                    print(bitcoinEUR.percentChange1h)
                    completion(bitcoinEUR, nil)
                }
            } catch {
                print(error)
            }
        }
        task.resume()
    }

}

控制台正在打印“未返回任何数据或数据未正确解码”。

编辑:这是代码的最新版本。 EDIT2:代码现在100%正常运行! :)

1 个答案:

答案 0 :(得分:2)

主要问题是价格和百分比是字符串而不是双打。顺便说一下,它返回一个数组,因此在解码时需要使用[Bitcoin].self类型:

这是你的可编码结构的样子:

struct Bitcoin: Codable {
    let id: String
    let name: String
    let symbol: String
    let rank: String
    let priceUSD: String
    let priceBTC: String
    let volume24hUSD: String
    let marketCapUSD: String
    let availableSupply: String
    let totalSupply: String
    let maxSupply: String
    let percentChange1h: String
    let percentChange24h: String
    let percentChange7d: String
    let lastUpdated: String
    let priceEUR: String
    let volume24hEUR: String
    let marketCapEUR: String
    private enum CodingKeys: String, CodingKey {
        case id, name, symbol, rank,
        priceUSD = "price_usd",
        priceBTC = "price_btc",
        volume24hUSD = "24h_volume_usd",
        marketCapUSD = "market_cap_usd",
        availableSupply = "available_supply",
        totalSupply = "total_supply",
        maxSupply = "max_supply",
        percentChange1h = "percent_change_1h",
        percentChange24h = "percent_change_24h",
        percentChange7d = "percent_change_7d",
        lastUpdated = "last_updated",
        priceEUR = "price_eur",
        volume24hEUR = "24h_volume_eur",
        marketCapEUR = "market_cap_eur"
    }
}

这就是你应该如何解码API返回的json数组并得到它的第一个元素:

do {
    if let bitcoinEUR = try JSONDecoder().decode([Bitcoin].self, from: data).first {
        print(bitcoinEUR)
        print(bitcoinEUR.priceEUR)
        print(bitcoinEUR.percentChange1h)
    }
} catch {
    print(error)
}

如果您只对这两个属性感兴趣,可以像这样设置比特币结构:

struct Bitcoin: Codable {
    let percentChange1h: String
    let priceEUR: String
    private enum CodingKeys: String, CodingKey {
        case percentChange1h = "percent_change_1h", priceEUR = "price_eur"
    }
}

修改/更新

注意:您使用美元货币符号显示欧元价格。如果您需要使用2个小数位格式化欧元价格,则需要首先使用API​​返回的字符串初始化新的浮点对象。

因此,您可以使用两个计算属性扩展比特币API,一个用于将欧元价格字符串转换为十进制,另一个用于将十进制值格式化为货币:

extension Bitcoin {
    var priceEURdecimal: Decimal {
        return Decimal(string: priceEUR) ?? 0
    }
    var priceEURcurrency: String {
        Formatter.currency.locale = Locale(identifier: "fr_FR")
        return Formatter.currency.string(for: priceEURdecimal) ?? ""
    }
}

您还需要将这些扩展名添加到项目中的新Swift文件中,以帮助您格式化货币:

extension NumberFormatter {
    convenience init(numberStyle: Style) {
        self.init()
        self.numberStyle = numberStyle
    }
}
extension Formatter {
    static let currency = NumberFormatter(numberStyle: .currency)
}

用法:

do {
    if let bitcoinEUR = try JSONDecoder().decode([Bitcoin].self, from: data).first {
        print(bitcoinEUR.priceEURdecimal)   // "13823.952495\n"
        print(bitcoinEUR.priceEURcurrency)  // "13 823,95 €\
    }
} catch {
    print(error)
}