歡迎加入QQ討論群258996829

Swift JSON 解析類庫 CaesarParser

發(fā)布時(shí)間:2016-04-19 23:39  回復(fù):0  查看:4080  感興趣:3  贊:0   最后回復(fù):2016-04-19 23:39  

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
    }
}

相關(guān)開源代碼

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?