Therefore, jsonString here actually stores a [String: Any], even thought the compiler thinks it is of type Any:let jsonString = try? JSONSerialization.jsonObject(with: data!, options: [])print(jsonString!)如果您尝试将其传递给接受 String 的 convertToDictionary,它当然不会起作用,因为字典和 String是不兼容的类型.If you try to pass this to convertToDictionary, which accepts a String, it of course will not work, because a dictionary and String are not compatible types.如何解决这个问题?问题已经解决了!您根本不需要 convertToDictionary.jsonString 本身是你想要的字典.The problem is already solved! You don't need convertToDictionary at all. jsonString itself is the dictionary you wanted.这是你需要做的:let jsonString = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any] ^^^^^^^^^^^^^^^^^ Add this part之后,您可以在 jsonString 上调用字典方法.我还建议您将 jsonString 重命名为其他名称.After that you can call dictionary methods on jsonString. I also suggest you to rename jsonString to something else. 这篇关于在 Swift 3 中将 JSON 字符串转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-24 05:17