Untitled

 avatar
unknown
swift
2 years ago
729 B
3
Indexable
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)
    }
    
}
Editor is loading...