CaesarParser 是一個(gè) JSON 解析類庫,用 Swift 編寫。
示例代碼:
enum Gender: Int { case Unknown = 0 case Male = 1 case Female = 2 } class Person: JSONDeserializable, JSONSerializable { var name: String? var age: Int? var birthday: Double? var weight: Float? var adult: Bool = false var gender: Gender = .Unknown var girlFriend: Person? var friends = [Person]() var luckyNumbers = [Int]() var favouredSingers = [String: Person]() var vips = [Int: Person]() var preferNumbers = [Int: Int]() var orientation = [Gender]() init(json: JSONDictionary) { name <-- json["name"] age <-- json["age"] birthday <-- json["birthday"] weight <-- json["weight"] adult <-- json["adult"] gender <-- json["gender"] girlFriend <-- json["girlFriend"] friends <-- json["friends"] luckyNumbers <-- json["luckyNumbers"] favouredSingers <-- json["favouredSingers"] vips <-- json["vips"] preferNumbers <-- json["preferNumbers"] orientation <-- json["orientation"] } func toJSONObject() -> JSONObject { var json = JSONDictionary() name --> json["name"] age --> json["age"] birthday --> json["birthday"] weight --> json["weight"] adult --> json["adult"] gender --> json["gender"] girlFriend --> json["girlFriend"] friends --> json["friends"] luckyNumbers --> json["luckyNumbers"] favouredSingers --> json["favouredSingers"] vips --> json["vips"] preferNumbers --> json["preferNumbers"] orientation --> json["orientation"] return json } }