tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.15k stars 1.03k forks source link

How to map 2 JSONs into 1 object #1005

Closed Nekokonab closed 5 years ago

Nekokonab commented 6 years ago

Your JSON dictionary:

First JSON { "name": "A", "skill": "B" }

Second JSON { "name" : nil "skill" : nil "ability" : [x , y , z , q ] }


### Your model:

```swift
struct monster : Mappable {
  var name: String!
  var skill : String!

  init(_ map: Map) {
    name <- map["name"]
    skill <- map["skill"]
  }
}

What you did:

map JSON , create one object from JSON data

What you expected:

I want to MAP 2 JSONs , create one Object instead

What you got:

1) Cant find a way to Map 2nd JSON to existing object which created from 1st JSON 2) Cant find a way to Map 1st + 2nd JSON = and produce new one object

Adrift001 commented 6 years ago

you can use rxswift Observable.combine to get two json, then map to one json, then produce new one object.

Ivan411 commented 5 years ago

What you did:

map JSON , create one object from JSON data

What you expected:

I want to MAP 2 JSONs , create one Object instead

What you got:

  1. Cant find a way to Map 2nd JSON to existing object which created from 1st JSON
  2. Cant find a way to Map 1st + 2nd JSON = and produce new one object

try this struct monster : Mappable { var name: String? var skill : String? var ability: [String]? init?(map: Map) { mapping(map: map) }

mutating func mapping(map: Map) { name <- map["name"] skill <- map["skill"] ability <- map["ability"] }

}