string to swift 2022

157

  let jsonText = "{\"first_name\":\"Sergey\"}"
        var dictonary:NSDictionary?
        
        if let data = jsonText.data(using: String.Encoding.utf8) {
            
            do {
                dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
            
                if let myDictionary = dictonary
                {
                     print(" First name is: \(myDictionary["first_name"]!)")
                }
            } catch let error as NSError {
                print(error)
            }
        }

Comments

Submit
0 Comments