get through the entire list REST JSON data

Hi everyone,

 

First Sorry about my english, I´m no English.

 

So, I am a swift Junior developer, and I have a question/problem:

 

How I get through the entire list REST JSON data?

 

{

“code”: “0”,

“description”: “ok”,

“whoAmI”: “BiciMad”,

“version”: “2.2.0 (noHash)”,

“time”: “19-11-2016 13:48:51.652”,

“data”: “{“stations”: [ {“id”: 1,”latitude”:”40.4168961″,”longitude”:”-3.7024255″,”name”:”Puerta del Sol A”,”light”: 0,”number”:”1a”,”address”:”Puerta del Sol nº 1″,”activate”: 1,”no_available”: 0,”total_bases”: 24,”dock_bikes”: 5,”free_bases”: 15,”reservations_count”: 0 }]}”

}

 

I can get “code, description, whoAmI, version, time” data with no problem, but when I want to get “data” data I have a problem and I don´t know how to get “id, latitude, longitude, name, etc, etc” data. I can´t get this last data.

 

Could you help me? please.

 

My source code from ViewController.swift:

 

 

import UIKit

class ViewController: UIViewController {
   
    @IBOutlet var txtBuscar: UITextField!
   
    @IBOutlet var txtNombre: UILabel!
   
    @IBOutlet var txtDireccion: UILabel!
   
    @IBOutlet var txtBicisAncladas: UILabel!
   
    @IBOutlet var txtBicisLibres: UILabel!
   
    @IBOutlet var txtReservasActivas: UILabel!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.quitarTeclado))
        view.addGestureRecognizer(tap)
    }
    func quitarTeclado(){
        view.endEditing(true)
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func btnBuscar(_ sender: Any) {
        quitarTeclado()
        let parada = txtBuscar.text
        let userKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        let passKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        let apiURL = "https://rbdata.emtmadrid.es:8443/BiciMad/get_single_station/(userKey)/(passKey)/(parada!)"
       
        guard let url = URL(string : apiURL) else {
            return
        }
       
        //le indicamos que los datos de la URL lo transforme en datos, sino da error
        guard let content = try? Data (contentsOf: url) else{
            print ("Error")
            return
        }
       
        //var json: AnyObject
        do {
            let json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
            print(json)
            let datosArray: AnyObject = (json!["data"] as? String)! as AnyObject
            print (datosArray)
            let estaciones: String = (datosArray["stations"] as? String)!
            print (estaciones)
        }catch{
            print("Error al coger los datos")
        }
       
    }
}

 

 

 

Thank you.

Powered by WPeMatico

About

You may also like...

Comments are closed.