Not displaying the selected JSON data

Benny

Very new to JSON. Manage to get the JSON data from the given api but just not able to display the selected data. Apparently the JSON parsing part is not working?

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {
    let baseURL = "https://free.currencyconverterapi.com/api/v6/convert?q=MYR_JPY"

    @IBOutlet weak var amountToConvert: UITextField!
    @IBOutlet weak var displayMYRtoJPY: UILabel!
    @IBOutlet weak var rateLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        GetcurrencyPair(url: baseURL)
    }

    func GetcurrencyPair(url: String) {
        Alamofire.request(baseURL, method: .get)
            .responseJSON { response in
                if response.result.isSuccess {
                    print("Sucess! Got the Currency Pair")
                    let currencyPairJSON : JSON = JSON(response.result.value!)

                    print(currencyPairJSON)

                    self.updateRateTicker(json: currencyPairJSON)
                } else {
                    print("Error: \(String(describing: response.result.error))")
                    self.rateLabel.text = "Connection Issues"
                }
        }
    }

    func updateRateTicker(json : JSON) {
        if let tempResult = json["results"].int {

            print(tempResult)
            rateLabel.text = String(tempResult)
        }
    }
}
vadian

Read the JSON. Except the value for key count there is no int value at all in the record.

Probably you are looking for the value for key val which is in the dictionary MYR_JPY and is a Double

Try

func updateRateTicker(json : JSON) {
    if let tempResult = json["results"]["MYR_JPY"]["val"].double {

        DispatchQueue.main.async {
            print(tempResult)
            rateLabel.text = String(tempResult)
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related