Untitled

mail@pastecode.io avatar
unknown
swift
a year ago
730 B
2
Indexable
Never
func convertToDictionary(text: String) -> [String: Any]? {
    if let data = text.data(using: .utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
        } catch {
            print(error.localizedDescription)
        }
    }
    return nil
}

func jsonToString(json: Dictionary<String, Any>){
    do {
        let data =  try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data
        let convertedString = String(data: data, encoding: String.Encoding.utf8) // the data will be converted to the string
    } catch let myJSONError {
        print(myJSONError)
    }
    
}