Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想用 Swift 4.1 解析 JSON
我知道这乍一看可能看起来很相似。
现在,Swift 4.1 和 Codable 为我们提供了一个很棒的工具。
但是我如何使用它/解析它 - 当我不知道结构时???
可以说我的 JSON 可以是
{ "foo" : "bar" }
并且
{ "foo" : { "baz" : "bar" }}
甚至简单到
{ "foo" : 1 }
那么我不能使用 Codable 对吗?还是我完全弄错了?
那怎么解析呢??
听起来您想将 JSON 数据转换为字典和数组——在这种情况下……</p>
do { if let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any] { // Do something with your dict } } catch { print("Error deserializing: \(error)") }