- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 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
键区分映射中的 Cat
和 Fish
?非常感谢!
最佳答案
{
"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" }
]
}
]
}
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/
请看一下我的代码。 int main () { Program* allcommand = new Program; allcommand->addCommand("add", new
因此,当我遇到调试断言时,我正在编写代码。现在我很想知道为什么这段代码不起作用: for(Model::MeshMap::iterator it = obj1->GetMeshes().begin()
这是我上一个问题的延续 Group, Sum byType then get diff using Java streams . 按照建议,我应该作为单独的线程发布,而不是更新原始线程。 因此,通过我
我正在实现一些非常适合 map 的代码。但是,我要迭代的列表中有大量对象,所以我的问题是哪种方法是解决此问题的最佳方法: var stuff = $.map(listOfMyObjects, some
我正在尝试创建一个包含不同类的成员函数指针的映射。成员函数都具有相同的签名。为了做到这一点,我所有的类都继承了一个 Object 类,它只有默认构造函数、虚拟析构函数和一个虚拟 ToString()
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: how do you make a heterogeneous boost::map? 有可能在 C++ 中
我有一个 Mysql 查询,请检查以下内容: SELECT `tbl_classSubjects`.`classID` , `tbl_classSubjects`.`sectionID` , `tbl
抱歉,这可能是一个基本问题。 JNA直接映射和接口(interface)映射有什么区别? 我的解释是否正确: 直接映射 : 直接使用库对象(如 Java 中的静态 main) 接口(interface
在 Twitter's Scala school collections section ,它们显示了一个带有偏函数作为值的 Map: // timesTwo() was defined earlie
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
据我了解,从 scala stdlib 声明一个映射并没有将其专门用于原始类型。我要的不是付出装箱/拆箱的代价,而是同时拥有scala map 的接口(interface)。一个明显的选择是使用 tr
如何为这样的 JSON 响应创建对象映射,它只是一个整数数组: [ 565195, 565309, 565261, 565515, 565292, 565281, 566346, 5
是否可以为 DTO 对象创建映射然后查询它们 而不是域?如果不解释为什么? 如果我需要几个 dtos 怎么办? DTos 是只读的 ID 由 NH 自动生成 将来这些 dtos 将设置映射到链接的 d
我有一个返回的函数(常规代码) [words: "one two", row: 23, col: 45] 在 Scala 中,我将上面更改为 Scala Map,但随后我被迫将其声明为 Map[Str
我有一组与 Vanilla 磅蛋糕烘焙相关的数据(200 行),具有 27 个特征,如下所示。标签caketaste是衡量烤蛋糕的好坏程度,由 bad(0) 定义, neutral(1) , good
我有试图映射到新代码的遗留代码。 OLD_PERSON pid sid name age NEW_PERSON pid sid fid age RESOLVE_PERSON pid fid statu
我有一个表,其中一个字段可以指向其他 3 个表之一中的外键,具体取决于鉴别器值是什么(Project、TimeKeep 或 CostCenter。通常这是用子类实现的,我想知道我有什么 注意子类名称与
我有一个类型 [ST s (Int, [Int])] 的绑定(bind)我正在尝试申请runST使用映射到每个元素,如下所示: name :: [ST s (Int, [Int])] --Of Cou
在我正在进行的项目中,我有以下实体:分析师、客户 和承包商。每个都继承自基类 User。 public abstract class User { public virtual int Id
我想知道是否可以在 Vim 中创建一个映射(对于普通模式),允许用户在映射执行之前输入。 我想为我最常用的 grep 命令创建一个快捷方式的映射。我希望命令允许输入我正在搜索的内容,然后在输入时执行。
我是一名优秀的程序员,十分优秀!