Click here to Skip to main content
15,868,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Swift
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: [])
           do {
               if (try JSONSerialization.jsonObject(with: data, options:[]) as? [String:Any]) != nil {
                   //print(json)
                   var ret = [String:Any]()
                   for json in ret{
                       print(ret["RagioneSociale"] as! String)
                   }
               }
           } catch let err{
               print(err.localizedDescription)
           }
           completion("")
       }
       task.resume()
Posted
Updated 28-Mar-18 0:02am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900