Hello guys, I have to read a json with swift formatted as follows:
{ IdCantiere: 324,
IdCliente: 171,
Filiale: 'SEDE',
RagioneSociale: '--',
NomeCantiere: '--',
DataCreazioneCantiere: 2018-01-25T17:47:21.643Z,
Tipologia: 'Consuntivo',
StatoCantiere: 'Chiuso',
StatoFatturazione: 0 },
{ IdCantiere: 329,
IdCliente: 271,
Filiale: 'SEDE',
RagioneSociale: '--',
NomeCantiere: '--',
DataCreazioneCantiere: 2018-01-30T10:00:42.227Z,
Tipologia: 'Consuntivo',
StatoCantiere: 'InCorso',
StatoFatturazione: 0 },
How can I read this json? I tried with the solution below but I print all the return json I would like to get the individual fields
What I have tried:
My Swift Code:
let db = Database()
let json: [String: Any] = ["NomeCantiere": "" + NomeCantiere]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
var request = URLRequest(url: URL(string: db.GetServerURL() + "/cantieri/ricerca")!)
request.httpMethod = "POST"
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
completion("")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
let read = responseJSON["return"]!
print("Valore: ",read)
completion("")
}
}
task.resume()
other tried:
let db = Database()
let json: [String: Any] = ["NomeCantiere": "" + NomeCantiere]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
var request = URLRequest(url: URL(string: db.GetServerURL() + "/cantieri/ricerca")!)
request.httpMethod = "POST"
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
completion("")
return
}
do {
if (try JSONSerialization.jsonObject(with: data, options:[]) as? [String:Any]) != nil {
var ret = [String:Any]()
for json in ret{
print(ret["RagioneSociale"] as! String)
}
}
} catch let err{
print(err.localizedDescription)
}
completion("")
}
task.resume()