前面的文章介绍了如何利用KakaJSON进行JSON转Model,从这篇文章开始介绍如何将Model转成JSON
生成JSON和JSONString struct Car: Convertible { var name: String = "Bently" var new: Bool = true var age: Int = 10 var area: Float = 0.12345678 var weight: Double = 0.1234567890123456 var height: Decimal = 0.123456789012345678901234567890123456789 var price: NSDecimalNumber = NSDecimalNumber(string: "0.123456789012345678901234567890123456789") var minSpeed: Double = 66.66 var maxSpeed: NSNumber = 77.77 var capacity: CGFloat = 88.88 var birthday: Date = Date(timeIntervalSince1970: 1565922866) var url: URL? = URL(string: "http://520suanfa.com") } let car = Car() // 将模型实例car转为JSON let json = car.kk.JSON() // 也可以调用全局函数`JSON(from:)` // let json = JSON(from: car) // 将模型实例car转为JSONString let jsonString = car.kk.JSONString() // 也可以调用全局函数`JSONString(from:)` // let jsonString = JSONString(from: car) /* {"birthday":1565922866,"new":true,"height":0.123456789012345678901234567890123456789,"weight":0.1234567890123456,"minSpeed":66.66,"price":0.123456789012345678901234567890123456789,"age":10,"name":"Bently","area":0.12345678,"maxSpeed":77.77,"capacity":88.88,"url":"http:\/\/520suanfa.com"} */ // 传入`true`代表生成格式化的JSONString let jsonString2 = car.kk.JSONString(prettyPrinted: true) /* { "height" : 0.123456789012345678901234567890123456789, "weight" : 0.1234567890123456, "minSpeed" : 66.66, "new" : true, "maxSpeed" : 77.77, "age" : 10, "capacity" : 88.88, "birthday" : 1565922866, "name" : "Bently", "price" : 0.123456789012345678901234567890123456789, "area" : 0.12345678, "url" : "http:\/\/520suanfa.com" } */ 可选类型 // 可选类型的问号?并不会影响JSON或者JSONString的生成 struct Student: Convertible, Equatable { var op1: Int? = 10 var op2: Double?? = 66.66 var op3: Float??? = 77.77 var op4: String???? = "Jack" var op5: Bool????? = true // op6可以是NSArray\Set<CGFloat>\NSSet\NSMutableArray\NSMutableSet类型 var op6: [CGFloat]?????? = [44.44, 55.55] } let jsonString = Student().kk.JSONString(prettyPrinted: true) /* { "op1" : 10, "op4" : "Jack", "op2" : 66.66, "op5" : true, "op6" : [ 44.44, 55.55 ], "op3" : 77.77 } */ Enum // 遵守了`ConvertibleEnum`协议、带有RawValue的Enum,能自动将RawValue转为JSON enum Grade: String, ConvertibleEnum { case perfect = "A" case great = "B" case good = "C" case bad = "D" } // Equatable is only for test cases, is not necessary for Model-To-JSON. struct Student: Convertible { var grade1: Grade = .great var grade2: Grade = .bad } let jsonString = Student().kk.JSONString() /* {"grade2":"D","grade1":"B"} */ 模型嵌套 struct Book: Convertible { var name: String = "" var price: Double = 0.0 } struct Car: Convertible { var name: String = "" var price: Double = 0.0 } struct Dog: Convertible { var name: String = "" var age: Int = 0 } struct Person: Convertible { var name: String = "Jack" var car: Car? = Car(name: "Bently", price: 106.666) var books: [Book]? = [ Book(name: "Fast C++", price: 666.6), Book(name: "Data Structure And Algorithm", price: 666.6), ] var dogs: [String: Dog]? = [ "dog0": Dog(name: "Wang", age: 5), "dog1": Dog(name: "ErHa", age: 3), ] } let jsonString = Person().kk.JSONString(prettyPrinted: true) /* { "dogs" : { "dog0" : { "name" : "Wang", "age" : 5 }, "dog1" : { "name" : "ErHa", "age" : 3 } }, "books" : [ { "price" : 666.6, "name" : "Fast C++" }, { "name" : "Data Structure And Algorithm", "price" : 666.6 } ], "name" : "Jack", "car" : { "price" : 106.666, "name" : "Bently" } } */ Any struct Book: Convertible { var name: String = "" var price: Double = 0.0 } struct Dog: Convertible { var name: String = "" var age: Int = 0 } struct Person: Convertible { // `books`也可以是NSArray\NSMutableArray var books: [Any]? = [ Book(name: "Fast C++", price: 666.6), Book(name: "Data Structure And Algorithm", price: 666.6), ] // `books`也可以是NSDictionary\NSMutableDictionary var dogs: [String: Any]? = [ "dog0": Dog(name: "Wang", age: 5), "dog1": Dog(name: "ErHa", age: 3), ] } /* { "dogs" : { "dog1" : { "age" : 3, "name" : "ErHa" }, "dog0" : { "age" : 5, "name" : "Wang" } }, "books" : [ { "name" : "Fast C++", "price" : 666.6 }, { "price" : 1666.6, "name" : "Data Structure And Algorithm" } ] } */ Model数组 struct Car: Convertible { var name: String = "" var price: Double = 0.0 } // `models`也可以是NSArray\NSMutableArray let models = [ Car(name: "BMW", price: 100.0), Car(name: "Audi", price: 70.0), Car(name: "Bently", price: 300.0) ] let jsonString = models.kk.JSONString(prettyPrinted: true) // 也可以使用全局函数`JSONString(from:prettyPrinted:)` // let jsonString = JSONString(from: models, prettyPrinted: true) /* [ { "name" : "BMW", "price" : 100 }, { "price" : 70, "name" : "Audi" }, { "price" : 300, "name" : "Bently" } ] */ Model Set struct Car: Convertible, Hashable { var name: String = "" var price: Double = 0.0 } let models: Set<Car> = [ Car(name: "BMW", price: 100.0), Car(name: "Audi", price: 70.0), Car(name: "Bently", price: 300.0) ] let jsonString = models.kk.JSONString(prettyPrinted: true) /* [ { "price" : 70, "name" : "Audi" }, { "price" : 300, "name" : "Bently" }, { "name" : "BMW", "price" : 100 } ] */ Key处理 struct Dog: Convertible { var nickName: String = "Wang" var price: Double = 100.6 // 可以通过实现`kk_JSONKey`方法自定义最终生成JSON的key func kk_JSONKey(from property: Property) -> JSONPropertyKey { switch property.name { case "nickName": return "_nick_name_" default: return property.name } } } let jsonString = Dog().kk.JSONString() /* {"price":100.6,"_nick_name_":"Wang"} */【KakaJSON手册】06_Model转JSON
内容版权声明:除非注明,否则皆为本站原创文章。