gpt4 book ai didi

json - ObjectMapper 如何基于 JSON 映射不同的对象

转载 作者:搜寻专家 更新时间:2023-10-30 22:05:24 24 4
gpt4 key购买 nike

我正在使用 ObjectMapper ( https://github.com/Hearst-DD/ObjectMapper ) 将我的 JSON 映射到 Swift 对象。

假设我有这个 JSON 结构:

{
animals: [
{
"type": "Cat",
"weight": 23,
"catchMice": true
},
{
"type": "Fish",
"weight": 1,
"swim": true
}
]
}

我有以下 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?
}

如何使用 JSON 对象中的 type 键区分映射中的 CatFish?非常感谢!

最佳答案

详情

  • Xcode 10.2.1 (10E1001)、Swift 5

json文件

{
"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" }
]
}
]
}

ObjectMapper 示例

Detect objects in array

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

Mapping classes

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

Helpers

extension Mappable {
var description: String {
return toJSONString(prettyPrint: true) ?? "\(self)"
}
}

Usage (read json from file)

    func sample() {
if let path = Bundle.main.path(forResource: "data", ofType: "json") {
do {
let text = try String(contentsOfFile: path, encoding: .utf8)
if let dict = try JSONSerialization.jsonObject(with: text.data(using: .utf8)!, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: Any] {
if let data = Animals(JSON: dict) {
print(data.animals.map {"class: \(type(of: $0))" }.joined(separator: ", ") )
// class: Cat, class: Fish, class: Animal
print("===============\n\(data)")
}
}
}catch {
print("\(error.localizedDescription)")
}
}
}

可编码示例

Detect objects in array

class Animals: Codable {

fileprivate enum CodingKeys: String, CodingKey {
case animals
}

private(set) var animals: [Animal]

required init(from decoder: Decoder) throws {
self.animals = []
let container = try decoder.container(keyedBy: CodingKeys.self)
var unkeyedDecodingContainer = try container.nestedUnkeyedContainer(forKey: .animals)
while !unkeyedDecodingContainer.isAtEnd {
if let obj = try? unkeyedDecodingContainer.decode(Cat.self) {
animals.append(obj)
continue
}

if let obj = try? unkeyedDecodingContainer.decode(Fish.self) {
animals.append(obj)
continue
}

if let obj = try? unkeyedDecodingContainer.decode(Animal.self) {
animals.append(obj)
continue
}
}
}
}

Mapping classes

enum AnimalType: String, Codable {
case cat = "cat", fish = "fish"
}

class BaseObject: Codable {
private(set) var id: Int?
private(set) var name: String?
}

class Animal: BaseObject {
private(set) var type: AnimalType?
private(set) var weight: Int?
private(set) var area: [String]?
private(set) var competence: [BaseObject]?

private enum CodingKeys: String, CodingKey {
case type, weight, area, competence
}

override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(type, forKey: .type)
try container.encodeIfPresent(weight, forKey: .weight)
try container.encodeIfPresent(area, forKey: .area)
try container.encodeIfPresent(competence, forKey: .competence)
try super.encode(to: encoder)
}

required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
type = try container.decodeIfPresent(AnimalType.self, forKey: .type)
weight = try container.decodeIfPresent(Int.self, forKey: .weight)
area = try container.decodeIfPresent([String].self, forKey: .area)
competence = try container.decodeIfPresent([BaseObject].self, forKey: .competence)
try super.init(from: decoder)
}
}

class Cat: Animal {
private(set) var canClimbTrees: Bool

private enum CodingKeys: String, CodingKey {
case canClimbTrees = "can_climb_trees"
}

override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(canClimbTrees, forKey: .canClimbTrees)
try super.encode(to: encoder)
}

required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.canClimbTrees = try container.decode(Bool.self, forKey: .canClimbTrees)
try super.init(from: decoder)
}
}


class Fish: Animal {

private(set) var canSwim: Bool

enum CodingKeys: String, CaseIterable, CodingKey {
case canSwim = "can_swim"
}

override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(canSwim, forKey: .canSwim)
try super.encode(to: encoder)
}

required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.canSwim = try container.decode(Bool.self, forKey: .canSwim)
try super.init(from: decoder)
}
}

Helpers

extension Decodable where Self : Encodable {

dynamic func format(options: JSONEncoder.OutputFormatting) -> String {
let encoder = JSONEncoder()
encoder.outputFormatting = options
do {
let jsonData = try encoder.encode(self)
if let jsonString = String(data: jsonData, encoding: .utf8) { return "\(jsonString)" }
} catch {
print("\(error.localizedDescription)")
}
return "nil"
}
}

Usage (read json from file)

func sample() {
if let path = Bundle.main.path(forResource: "data", ofType: "json") {
do {
guard let data = try String(contentsOfFile: path, encoding: .utf8).data(using: .utf8) else { return }
let decoder = JSONDecoder()
let result = try decoder.decode(Animals.self, from: data)
print(result.animals.map {"\(type(of: $0))" } )
//print("===============")
//print(result.format(options: .prettyPrinted))
} catch let error {
print("\(error.localizedDescription)")
}
}
}

输出

["猫", "鱼", "动物", "猫"]

关于json - ObjectMapper 如何基于 JSON 映射不同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43269448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com