我正在使用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对象中的键区分映射Cat
和Fish
映射type
?非常感谢!
{
"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" }
]
}
]
}
检测数组中的对象
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
}
}
}
映射类
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"]
}
}
帮手
extension Mappable {
var description: String {
return toJSONString(prettyPrint: true) ?? "\(self)"
}
}
用法(从文件读取json)
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)")
}
}
}
检测数组中的对象
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
}
}
}
}
映射类
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)
}
}
帮手
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"
}
}
用法(从文件读取json)
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)")
}
}
}
[“猫”,“鱼”,“动物”,“猫”]
问题内容: 我的一位同事向我提出了一个有趣的问题,但是我找不到一个整洁漂亮的Java 8解决方案。问题是流式传输POJO列表,然后基于多个属性将它们收集在映射中- 映射导致POJO多次发生 想象以下POJO: 将其设置为: 备选方案1 :在“ stream”之外(或在之外)使用。 备选方案2 :创建地图项并流式传输,然后流式传输。海事组织,这有点太冗长,不太容易阅读。 备选方案3 :到目前为止,这
我将json传递给ObjectMapper。JSON字符串如下所示: 我的类如下所示: 这种行为是意料之中的吗?如果是,有什么解决办法? 更新:添加了类描述。
我正在微服务中做一些更改,它有一个像以下这样的对象: } 我将从Json中获取我将要使用的信息。Json有一个名为的键,我想在两个不同的变量中使用这个速率,一个int-rate和一个Float-ratePPR。这里的问题是,我知道在同一个对象中不能有来自同一个Json的两个变量,但我有义务这样做,因为我不能触摸变量,所以我创建了变量,以便在需要时使用。 当我进行测试时,会出现以下错误: 属性“ra
问题内容: 我在了解如何实现这一目标方面遇到了问题。 基本上我们有一个API,用户发送以下格式的JSON :(如果代码不完美,请原谅,但您可以理解) 好的,我不确定我是否正确设置了JSON格式,但是现在这是我的问题。 我有一个带有参数Name,Last的类,还有一个作为其成员之一的对象Client和Property Date。 像这样: 因此,基本上,我不确定如何获取JSON,然后将其映射到我的对
我是java新手,正在尝试学习objectmapper。我正在使用它将地图转换为pojo。地图中的键是字符串,所有值都是字符串值,除了我想转换为地图的值。请仔细阅读下面的示例代码,以获得更清晰的图片。 POJO类: 测试代码: 例外: 尝试的变体选项: 我知道我可以将map1字段也保留为String,然后使用另一个对象映射器实例将其转换为map,但我想避免它。有没有办法直接将测试代码中的字符串转换
问题内容: 我们有一张有很多列的大桌子。移至MySQL Cluster后,由于以下原因无法创建表: 错误1118(42000):行大小太大。不包括BLOB在内的已使用表类型的最大行大小为14000。这包括存储开销,请查阅手册。您必须将某些列更改为TEXT或BLOB 举个例子: 这是用于存储配置参数的表。我在想,我们可以将一些列合并为一个列,并将其存储为JSON对象,然后将其转换为Java对象。 例