Skip to content

Instantly share code, notes, and snippets.

@Andy0570
Created November 18, 2020 03:19
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 Andy0570/d517b192abe33fd16016ad4e315012c6 to your computer and use it in GitHub Desktop.
Save Andy0570/d517b192abe33fd16016ad4e315012c6 to your computer and use it in GitHub Desktop.
JSON Data 解析
import Foundation
extension Data {
init?(json: Any) {
guard let data = try? JSONSerialization.data(withJSONObject: json, options: .fragmentsAllowed) else { return nil }
self.init(data)
}
func jsonToDictionary() -> [String: Any]? {
(try? JSONSerialization.jsonObject(with: self, options: .allowFragments)) as? [String: Any]
}
func jsonToArray() -> [Any]? {
(try? JSONSerialization.jsonObject(with: self, options: .allowFragments)) as? [Any]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment