7ba*_*all 7 json swift objectmapper
我正在使用ObjectMapper(https://github.com/Hearst-DD/ObjectMapper)将我的JSON映射到Swift对象.
假设我有这个JSON结构:
{
animals: [
{
"type": "Cat",
"weight": 23,
"catchMice": true
},
{
"type": "Fish",
"weight": 1,
"swim": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有以下Swift对象:
class Foo: Mappable {
var animals: [Animal] = []
func mapping(map: Map) {
animals <- map["animals"] //But I want to be able to distinguish between Cat and Fish objects here
}
}
class Animal: Mappable {
var type: String?
var weight: Double?
required init?(map: Map) {}
func mapping(map: Map) {
type <- map["type"]
weight <- map["weight"]
}
}
class Cat: Animal { // How do I make use of this class
var catchMice: Bool?
}
class Fish: Animal { // How do I make use of this class
var swim: Bool?
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能区分Cat并Fish在使用我的映射type在我的JSON对象的关键?非常感谢!
Vas*_*huk 14
xCode 9.1,Swift 4 xCode 8.3,Swift 3.1
AnimalsArrayTransformType
{
"animals": [
{
"id": 1,
"name": "Cat",
"type": "cat",
"weight": 23,
"area": ["home", "street"],
"can_climb_trees": true,
"competence": [
{ "id": 1, "name": "to catch mouse" },
{ "id": 2, "name": "to mew" },
{ "id": 3, "name": "to wake people up in the morning" },
{ "id": 4, "name": "to eat fish" }
]
},
{
"id": 2,
"name": "fish",
"type": "fish",
"weight": 1,
"area": ["ocean", "lake"],
"can_swim": false,
"competence": [
{ "id": 5, "name": "to swim" },
{ "id": 6, "name": "to tease a cat" }
]
},
{
"id": 3,
"name": "dog",
"weight": 55,
"area": ["house", "street"],
"competence": [
{ "id": 5, "name": "to bring newspaper" },
{ "id": 6, "name": "to a good booy" }
]
},
{
"id": 4,
"name": "Cat",
"type": "cat",
"weight": 23,
"area": ["home", "street"],
"can_climb_trees": true,
"competence": [
{ "id": 1, "name": "to catch mouse" },
{ "id": 2, "name": "to mew" },
{ "id": 3, "name": "to wake people up in the morning" },
{ "id": 4, "name": "to eat fish" }
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
映射类
import Foundation
import ObjectMapper
class AnimalsArrayTransformType: TransformType {
public typealias Object = [Animal]
public typealias JSON = [[String: Any]]
func transformToJSON(_ value: [Animal]?) -> [[String : Any]]? {
guard let animals = value else { return nil }
return animals.map { $0.toJSON() }
}
func transformFromJSON(_ value: Any?) -> [Animal]? {
guard let animals = value as? [[String: Any]] else { return nil }
return animals.compactMap { dictionary -> Animal? in
if let cat = Cat(JSON: dictionary) { return cat }
if let fish = Fish(JSON: dictionary) { return fish }
if let animal = Animal(JSON: dictionary) { return animal }
return nil
}
}
}
Run Code Online (Sandbox Code Playgroud)
import Foundation
import ObjectMapper
class Animals: Mappable, CustomStringConvertible {
private(set) var animals: [Animal] = []
required init?(map: Map) { }
func mapping(map: Map) {
animals <- (map["animals"], AnimalsArrayTransformType())
}
}
class BaseObject: Mappable, CustomStringConvertible {
private(set) var id: Int?
private(set) var name: String?
required init?(map: Map) { mapping(map: map) }
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
}
}
class Animal: BaseObject {
private(set) var type: String?
private(set) var weight: Double?
private(set) var area: [String]?
private(set) var competence: [BaseObject]?
required init?(map: Map) { super.init(map: map) }
override func mapping(map: Map) {
super.mapping(map: map)
type <- map["type"]
weight <- map["weight"]
area <- map["area"]
competence <- map["competence"]
}
}
class Cat: Animal {
private(set) var canClimbTrees: Bool?
required init?(map: Map) {
super.init(map: map)
if canClimbTrees == nil { return nil }
}
override func mapping(map: Map) {
super.mapping(map: map)
canClimbTrees <- map["can_climb_trees"]
}
}
class Fish: Animal {
private(set) var canSwim: Bool?
required init?(map: Map) {
super.init(map: map)
if canSwim == nil { return nil }
}
override func mapping(map: Map) {
super.mapping(map: map)
canSwim <- map["can_swim"]
}
}
Run Code Online (Sandbox Code Playgroud)
extension Mappable {
var description: String {
return toJSONString(prettyPrint: true) ?? "\(self)"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17333 次 |
| 最近记录: |