Swift51.com
Swift 头像
Swift  2016-04-19 23:39

Swift JSON 解析类库 CaesarParser

回复:0  查看:4117  感兴趣:3  赞:0  

CaesarParser 是一个 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
    }
}

相关开源代码