Hi programmers, i had this problem.
this is my Json
echo '{
"ID":"'.$row[0].'",
"Nombre":"'.$row[3].'",
"Matricula":"'.$row[7].'",
"Vehiculo":"'.$row[8].'",
"Modelo":"'.$row[9].'",
"Identificacion":"'.$row[10].'",
"Distancia":"'.$Distancia.'",
},';
this is the data from a "driver in my app" the Json will auto-Increase every time a "driver" log in and it will look like this when gets call with the PHP
echo '{
"ID":"'.$row[0].'",
"Nombre":"'.$row[3].'",
"Matricula":"'.$row[7].'",
"Vehiculo":"'.$row[8].'",
"Modelo":"'.$row[9].'",
"Identificacion":"'.$row[10].'",
"Distancia":"'.$Distancia.'",
},'; // as you can see it is separeted with a ','
"ID":"'.$row[0].'",
"Nombre":"'.$row[3].'",
"Matricula":"'.$row[7].'",
"Vehiculo":"'.$row[8].'",
"Modelo":"'.$row[9].'",
"Identificacion":"'.$row[10].'",
"Distancia":"'.$Distancia.'",
},';
So, i want to know how to separate in swift the income-data
func iamarjson()
{
let myURL = URL(string: "he he (michael jackson")
var request = URLRequest(url: myURL!)
request.httpMethod = "POST"
let posString = "Client_lat=\(coordLat.text!)&Client_lon=\(coordLong.text!)"
request.httpBody = posString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
if let error = error {
print("error=\(error)")
return
}
guard let data = data else {
print("Something wrong")
return
}
// this is my Json
struct Blog: Decodable {
let Response: String
let articles: [Article]
enum CodingKeys : String, CodingKey {
case Response
case articles = "Perfil"
}
}
struct Article: Decodable {
let ID: String
let Foto: String
let Nombre: String
let Matricula: String
let Vehiculo: String
let Modelo: String
let Identificacion: String
let Distancia: String
// as you can see, is a structure for 1 json data, but it will be displayed with a ',' if theres more than 1 "driver"
}
do {
let blog = try JSONDecoder().decode(Blog.self, from: data)
DispatchQueue.main.async {
if blog.Response == "true" {
for article in blog.articles {
let id = article.ID
UserDefaults.standard.set(id, forKey: "idJson")
let foto = article.Foto
UserDefaults.standard.set(foto, forKey: "fotoJson")
let nombre = article.Nombre
UserDefaults.standard.set(nombre, forKey: "nombreJson")
let matricula = article.Matricula
UserDefaults.standard.set(matricula, forKey: "matriJson")
let vehiculo = article.Vehiculo
UserDefaults.standard.set(vehiculo, forKey: "vehiJson")
let modelo = article.Modelo
UserDefaults.standard.set(modelo, forKey: "modeloJson")
let identificacion = article.Identificacion
UserDefaults.standard.set(identificacion, forKey: "identiJson")
let distancia = article.Distancia
UserDefaults.standard.set(distancia, forKey: "distaJson")
let userImage = UserDefaults.standard.string(forKey: "fotoJson")
let imageBase64 = UnaVezLogeadoViewController.convertBase64ToImage(imageString: userImage!)
self.imagenConductor.image = imageBase64
/*
let nombre = UserDefaults.standard.string(forKey: "nombreJson")
let matricula = UserDefaults.standard.string(forKey: "matriJson")
let vehiculo = UserDefaults.standard.string(forKey: "vehiJson")
let modelo = UserDefaults.standard.string(forKey: "modeloJson")
let identificacion = UserDefaults.standard.string(forKey: "identiJson")
let distancia = UserDefaults.standard.string(forKey: "distaJson")
*/
self.nombreConductor.text = nombre
self.matriculaConductor.text = matricula
self.vehiculoConductor.text = vehiculo
self.modeloConductor.text = modelo
self.numeroTaxiConductor.text = identificacion
self.distanciaConductor.text = distancia
break
}
} else if blog.Response == "false" {
self.displayAlert(title: "Usuario", message: "no hay conductores disponibles")
} else if blog.Response == "nothing" {
self.displayAlert(title: "Información", message: "error red")
}
}
} catch {
print("Error: Couldn't decode data into Blog:", error)
return
}
}
task.resume()
}
what do i need to do for separate the income-data in my swfit code?.
what i have tried?
delete from my DB all drivers and kept 1 and it works, as i said in the comments it works for one driver.