Help with json swift on ios

2

Greetings to Everyone

I'm trying to access the values of the Result

{"Result":{"CODIGO":"Error","DESCRIPCION":"true","ID":"23"}}

The Controller, for now I only access the Result.

class FirstViewController: UIViewController {

@IBOutlet weak var txtCarta: UITextField!
@IBOutlet weak var botonEnviar: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

   @IBAction func Mostrar(sender: UIButton) {

    let envio = txtCarta.text!
    let body = "codigo="+envio

    let myUrl = NSURL(string: "AquiVaMiUrl");
    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {


        data, response, error in

        if error != nil
        {
            print("error=\(error)")
            return
        }


        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(responseString!)


        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)

            // aqui solo accedo a Result pero me falta obtener sus valores.
          let blogs = json["Result"]!
           print(blogs!)


        } catch {
            print("error serializing JSON: \(error)")
        }


    }

    task.resume()


}

}

Thank you.

    
asked by Rafa Pinto 30.04.2016 в 19:38
source

4 answers

4

You can do the following to make sure that all keys/values exist:

if let blogs = json["Result"] {

    if let codigo =  blogs?["CODIGO"] {
        print(codigo)
    }

    if let desc =  blogs?["DESCRIPCION"] {
        print(desc)
    }

    if let id =  blogs?["ID"] {
        print(id)
    }

}
    
answered by 30.04.2016 / 20:06
source
1

The first thing you have to do is convert the json of the result to a dictionary:

let blogs = json["Result"] as! NSDictionary

and you can collect the data using:

let id = blogs["id"]
    
answered by 22.05.2016 в 20:22
1

You can use the swifty json library, I leave you the link: link

and an example of use:

if let dataFromString = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
    let json = JSON(data: dataFromString)
let name = json["name"].stringValue
}

I hope you serve

    
answered by 11.08.2016 в 14:16
1

If you are using JSON , try using the Alamofire library. I use it and it simplifies things a lot.

I leave a link: link

I show you your code if you use this library:

import Alamofire

class FirstViewController: UIViewController {

    @IBOutlet weak var txtCarta: UITextField!
    @IBOutlet weak var botonEnviar: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func Mostrar(sender: UIButton) {

        let envio = txtCarta.text!
        //let body = "codigo="+envio
        let parametros = ["codigo": envio]

        let myUrl = NSURL(string: "AquiVaMiUrl")

        Alamofire.request(.POST, myUrl, parameters: parametros).responseJSON { response in
            if let respuesta = response.data {
                json = JSON(data: respuesta)

                let blogs = json["Result"]!
                print(blogs!)
            }
        }
    }
}
    
answered by 11.08.2016 в 14:10