1 Reply
      Latest reply: Dec 16, 2016 4:59 AM by eskimo RSS
      sleyterach Level 1 Level 1 (0 points)

        Hi everyone...

        I have been doing queries to database and I get an array of json with String format, and I need to  convert (downcasting) to Array or Dictionary to create a TableView with the data got previously

         

        func closeTaskService(_ valor: String, completionHandler: (_ response:NSString) -> ()) {
                let request = NSMutableURLRequest(url: URL(string: "http:/ 
                let db = self.dato.object(forKey: "dbName")
                let db1 = db as AnyObject? as? String
                let dbName : String = (db1 as AnyObject? as? String)!
                request.httpMethod = "POST"
                let postParams = "DB_name="+dbName+"&"+"valor=%"+valor+"%"
                / 
                request.httpBody = postParams.data(using: String.Encoding.utf8)
                let task = URLSession.shared.dataTask(with: request as URLRequest) {
                    (data, response, error ) in
                    / 
                    let responseString: String = String (data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
                    / 
                    
                    let numbers = responseString.replacingOccurrences(of: "[", with: "", options: .literal, range: nil)
                    let numbers2 = numbers.replacingOccurrences(of: "]", with: "", options: .literal, range: nil)
                    / 
                    / 
                    let myArray : [String] = numbers2.components(separatedBy: "},{")
                    print(myArray)
                    
                    
                    for array in myArray{
                        print(array)
                        print(myArray[1])
                    }
                }
                task.resume()
            
            }
        
        • Re: Data for TableView
          eskimo Apple Staff Apple Staff (6,470 points)

          Parsing JSON by hand is an exercise fraught with peril; you should take a look at [JSONSerialization][refJ|ADD]

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"