Skip to content

Instantly share code, notes, and snippets.

@liuzhida33
Created July 11, 2020 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuzhida33/c59ff2df0241a6cab7fd691b75bb3587 to your computer and use it in GitHub Desktop.
Save liuzhida33/c59ff2df0241a6cab7fd691b75bb3587 to your computer and use it in GitHub Desktop.
如何在Swift中轻松解析深度json
public func resolve<T>(_ jsonDictionary: [String: Any], keyPath: String) -> T? {
var current: Any? = jsonDictionary
keyPath.split(separator: ".").forEach { component in
if let maybeInt = Int(component), let array = current as? Array<Any> {
current = array[maybeInt]
} else if let dictionary = current as? JSONDictionary {
current = dictionary[String(component)]
}
}
return current as? T
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment