无法从API解码JSON数据

问题描述:

我正在尝试使用从API获取的数据更新标签,以打印应用程序中的比特币价格和百分比变化,但是我不知道如何正确解码JSON.

I am trying to update labels with data fetched from an API to print Bitcoin price and percentage variation in an app but I can't figure out how to properly decode the JSON.

BitcoinInfo.swift:

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:

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

}

控制台正在打印未返回任何数据或未正确解码数据."

The console is printing "Either no data was returned or data was not properly decoded."

这是代码的最新版本. 该代码现在100%可用! :)

This is the latest version of the code. The code is now 100% functional! :)

主要问题是价格和百分比是Strings而不是Doubles.顺便说一句,它返回一个数组,因此解码时需要使用[Bitcoin].self类型:

The main problem is the price and percentage are Strings not Doubles. Btw it returns an array so you need to use [Bitcoin].self type when decoding it:

这是您的可编码结构的外观:

This is how your codable struct should look like:

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数组并获取其第一个元素的方法:


And this is how you should decode the json array returned by the API and get its first element:

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


如果您仅对这两个属性感兴趣,可以按如下所示设置您的比特币结构:


If you are only interested in those two properties you can set your bitcoin structure like this:

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


编辑/更新:

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

Note: You are displaying euro price using the dollar currency symbol. If you need to format your euro price with 2 fraction digits you will need to initialize first a new Floating point object with the string returned by the API.

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

So you can extend the Bitcoin API with two computed properties, one to convert the euro price string to Decimal and the other to format the decimal value into currency:

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文件中,以帮助您设置货币格式:

You will need also to add those extensions to a new Swift file in your project to help you format the currency:

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


用法:


Usage:

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