This is the JSON
one I have:
Optional({
Cuenta = 1;
Datos = (
{
"usr_apemat" = "";
"usr_apepat" = damas;
"usr_celular" = 0;
"usr_codigopostal" = 123457;
"usr_direccion" = Campeche;
"usr_email" = "[email protected]";
"usr_fechanac" = "0000-00-00 00:00:00";
"usr_fotolink" = "";
"usr_id" = 7;
"usr_idestado" = 0;
"usr_idpais" = 0;
"usr_idsangre" = 0;
"usr_idsexo" = 0;
"usr_nombre" = pedro;
"usr_password" = damas;
"usr_rfc" = "";
"usr_swbaja" = 0;
"usr_telefono" = 1278932;
"usr_username" = damas;
}
);
Error = 0;
Mensaje = "Se Realizo la consulta con exito";
Pasaporte = {
};
nPasaporte = 0;
})
and this is my code:
let usr_username = usernameTxt.text!.lowercased()
let usr_password = passwordTxt.text!
let parameters = ["Accion": 2, "Datos":[ "usr_username": usr_username, "usr_password": usr_password]] as [String : Any]
// send request to mysql db
// url to access our php file
let url = URL(string: "http://sipot.conanp.gob.mx/server/pasaportes.php")!
let session = URLSession.shared
// request url
var request = URLRequest(url: url)
// method to pass data POST - cause it is secured
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
// no error
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]
guard let parseJSON = json else {
print("Error while parsing")
return
}
// si mando a imprimir id el json me devuelve solo los datos pero de alli ya no puedo entrar a los datos generales del usuario
let id = parseJSON["Datos"]
// let datoJson = id["usr_nombre"] as! [AnyObject]?
// print("hola 3",id)
//let datoJson = id?["usr_nombre"] as? String
// let arrayJSON = id?["usr_nombre"] as? String
// print("print 1",datoJson)
// successfully logged in
if id != nil {
print("hola 3",datoJson)
// save user information we received from our host
UserDefaults.standard.set(parseJSON, forKey: "parseJSON")
user = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary
print("hola 1", user)
// go to tabbar / home page
DispatchQueue.main.async(execute: {
appDelegate.login()
})
// error
} else {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = parseJSON["Mensaje"] as! String
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
} catch {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error)"
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
} else {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
})
task.resume()
}
}
you can access directly using
But it is always cumbersome. I recommend that you create an entity and map it (especially if it is a recurring one, that is, you are going to use it several times and so you could access it in the following way.
Entidad.datos.userMail <- This will be much simpler and cleaner
you could do it this way