CaesarParser
CaesarParser 是一个框架采用 Swift 开发,用于解析JSON模型。
特性 要Cocoa Touch Framework requires iOS 8 or later.
Manual add CaesarParser to your project requires iOS 7 or later.
基本用法Any type that confirm Deserializable or Convertible protocol can be parse. Besides you can use custom value converter during parsing.
/// Use for Class, Nested Type public protocol Deserializable { init(json: JSONDictionary) } /// Use for Primitive Type public protocol Convertible { static func convert(json: JSONObject) -> Self? }
Any type that confirm Serializable can be parse to JSON.
/// convert to JSON object public protocol Serializable { func toJSONObject() -> JSONObject }
Build-in Support
Demo Code
enum Sex: Int { case Unknown = 0 case Male = 1 case Female = 2 } class Person: Deserializable, Serializable { var name: String var age: Int var birthday: Double var weight: Float var adult: Bool = false var sex: Sex = .Unknown var girlFriend: Person? var friends = [Person]() var luckyNumbers = [Int]() var favouredSingers = [String: Person]() var vips = [Int: Person]() var preferNumbers = [Int: Int]() var orientation = [Sex]() init(json: JSONDictionary) { name <-- json["name"] age <-- json["age"] birthday <-- json["birthday"] weight <-- json["weight"] adult <-- json["adult"] sex <-- json["sex"] 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"] sex --> json["sex"] girlFriend --> json["girlFriend"] friends --> json["friends"] luckyNumbers --> json["luckyNumbers"] favouredSingers --> json["favouredSingers"] vips --> json["vips"] preferNumbers --> json["preferNumbers"] orientation --> json["orientation"] return json } }
AcknowledgementsCaesarParser is available under the MIT license.
项目地址: https://github.com/lancy/CaesarParser
扩展阅读ObjectFromJSON - swift版本的JSON解析Swift的JSON 解析库:Gloss
JSONExport:将 JSON 对象导出成模型类的 Mac OS X 桌面应用(Swift)
Swift 简洁的JSON辅助库:BYJSON
如何在 Swift 中优雅地处理 JSON
为您推荐Vue.js基本语法的介绍
Node.js 风格指南
使用Bootstrap与ShieldUI创建销售业绩仪表板
Dojo Mobile 1.7 入门
更多CaesarParser
Apple Swift开发
同类热门新闻
同类热门经验
阅读目录