Upvote:75

Aprove answer
do {
    if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] {
        print(jsonResult)
    }
} catch let error as NSError {
    print(error.localizedDescription)
}

Upvote:5

do {
    let message = try NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)
    if let dict = message as? NSDictionary {
        // ... process the data
    }
} catch let error as NSError {
    print("An error occurred: \(error)")
}

More Answer related to the Same Query

Upvote:0

 do{
            if let responseObj = try JSONSerialization.jsonObject(with: results, options: .allowFragments) as? NSDictionary{

                if JSONSerialization.isValidJSONObject(responseObj){
                    //Do your stuff here
                }
                else{
                    //Handle error
                }
            }
            else{
                //Do your stuff here
            }
        }
        catch let error as NSError {
                print("An error occurred: \(error)") }

Credit Goes to: stackoverflow.com

Related question with same questions but different answers