Created
November 18, 2020 03:19
-
-
Save Andy0570/d517b192abe33fd16016ad4e315012c6 to your computer and use it in GitHub Desktop.
JSON Data 解析
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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