Search code examples
iosarraysjsonswift

How to change data of some parameters in a Json with swift


I am working on a project which it is reading a json from an API which it provides currency of different countries and the output is like this.

{
base = EUR;
date = "2019-08-03";
rates =     {
AED = "4.08777";
AFN = "89.092349";
ALL = "121.761514";
AMD = "529.6588400000001";
ANG = "1.985008";
AOA = "391.621548";
ARS = "49.530913";
AUD = "1.636268";
AWG = "2.003316";
AZN = "1.897631";
BAM = "1.960138";
BBD = "2.245272";
BDT = "94.033858";
BGN = "1.960138";
BHD = "0.419517";
        .
        .
        .
     };
    success = 1;
    timestamp = 1564832346;
   }

as you see the base is EUR which stands for euro, I want to change the base to USD.

here is my codes:

 override func viewDidLoad() {
    
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
   
    pickerView.dataSource = self
    pickerView.delegate = self
    
    //Getting data
    
    print("Hello Wrold")
    let url = URL(string: "http://data.fixer.io/api/latest?access_key=")
    print(url!)
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        
        print("Hello Wrold3")
        if (error != nil)
        {
            print("ERROR")
        }
        else
        {
            print("Hello Wrold2")
            if let content = data
            {
                do
                {
                    let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    
                    print(myJson)
                   
                }
                catch
                {
                    
                }
            }
        }
    }
    task.resume()
    
 
    
    
}

I tried to reach it by writing myJson.base but seems it does not understand it. Does any one have any idea how to do it?


Solution

  • The question has nothing to do with JSON, but how to tell the api to use a different base currency.

    https://data.fixer.io/api/latest
        ? access_key = API_KEY
        & base = USD
    

    but checking with your key, I receive this

    {"success":false,"error":{"code":105,"type":"base_currency_access_restricted"}}
    

    with this explanation for the error code

    105 The current subscription plan does not support this API endpoint.