So I am having trouble with Swift 3. I am sending a get call to my server and I get the data back that I expect. However I can't figure out how to create objects from the jsonArray so I can easily access the data.
This is my func so far:
func getAllRoles(refreshToken: String){
print("vi kører getAllRoles \(refreshToken)")
let urlPath = "http://keebintest-pagh.rhcloud.com/api/users/allroles/"
let url = NSURL(string: urlPath)
let session = URLSession.shared
let request = NSMutableURLRequest(url: url as! URL)
request.addValue(refreshToken, forHTTPHeaderField: "refreshToken")
request.httpMethod = "GET"
let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
print("Task completed \(data) and response is xxxx")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString!)")
// Her laver vi data om til en dictionary
// let s = (try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers)) as! String
// print("det virkede?")
// print(s)
// var json: Array<String>!
// do {
// json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions()) as? Array
// } catch {
// print(error)
// }
//
// print("her er mit json \(json[0])")
})
task.resume()
}
This is what my console log looks like:
Task completed Optional(56 bytes) and response is xxxx
responseString = [{"id":1,"roleName":"Admin"},{"id":2,"roleName":"User"}]
here is my json {
id = 2;
roleName = User;
}
So obviously I get my data, and it is in an array which I can access. However I can't figure out how to create jsonObjects from each of those in the array. Could someone help?
Thanks in advance!