What you need to do is use JSONSerialization
to get your JSON
object from data
. Also in Swift 3 use URLRequest
and URL
instead of NSMutableURLRequest
and NSURL
.
func requestPost () {
var request = URLRequest(url: URL(string: "https://oakkohost.000webhostapp.com/test.php")!)
request.httpMethod = "POST"
let postString = "numQue"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let array = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [[String:Any]],
let obj = array.first {
let id = obj["id"] as? String
//Set id to label on main thread
DispatchQueue.main.async {
self.yourIdLabel.text = id
}
}
}
task.resume()
}
0
solved How to get data from php as JSON and display on label swift [closed]