Avatar ·

How to convert a JSON string to a Dictionary in Swift?

Hi everyone! I'm new to Swift and ran into a problem working with JSON. I receive a JSON string from the server, for example: `{"name":"John","age":30,"city":"New York"}`. I need to convert this string to a dictionary `[String: Any]` so I can access values by keys. I tried using `JSONSerialization`, but I can't get it to work. Here's my code: ```swift let jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}" if let jsonData = jsonString.data(using: .utf8) { do { let dict = try JSONSerialization.jsonObject(with: jsonData, options: []) print(dict) } catch { print("Error: \(error.localizedDescription)") } } ``` This code prints the dictionary to the console, but Xcode complains that `dict` is of type `Any`