使用 AlamofireObjectMapper 将对象 json 数据映射到 2 个单独的数组中
Posted
技术标签:
【中文标题】使用 AlamofireObjectMapper 将对象 json 数据映射到 2 个单独的数组中【英文标题】:Mapping object json data into 2 separate arrays with AlamofireObjectMapper 【发布时间】:2017-12-21 08:02:50 【问题描述】:我目前正在尝试实现到我的模型的映射,而我目前的做法并没有产生任何结果。我有一个 MediaLibrary 类,它包含 2 个数组,“exerciseMedia”和“stretchMedia”,它们都包含一个“媒体”对象数组。我选择了 ObjectMapper/AlamofireObjectMapper 库来将数据映射到我的模型。
import ObjectMapper
class MediaLibrary: Mappable
var exerciseMedia: [Media]?
var stretchMedia: [Media]?
required init?(map: Map)
mapping(map: map)
func mapping(map: Map)
exerciseMedia <- map["ExerciseMedia"]
stretchMedia <- map["StretchMedia"]
这里是 Media 对象,它将构成填充我在 MediaLibrary 类中的两个数组的对象。
import ObjectMapper
struct Media: Mappable
var _id: String?
var name: String?
var desc: String?
var imageURI: String?
var videoURI: String?
var trainerID: String?
var isShoulders : Bool?
var isNeck : Bool?
var isLegs : Bool?
var isHands : Bool?
var isChest : Bool?
var isCalves : Bool?
var isButt : Bool?
var isBack : Bool?
var isArms : Bool?
var isCore : Bool?
var isFullBody : Bool?
var isLowerBody : Bool?
var isUpperBody : Bool?
var isExercise : Bool?
var isStretch : Bool?
var equipmentNeeded : String?
init?(map: Map)
mutating func mapping(map: Map)
_id <- map["_id"]
name <- map["name"]
desc <- map["desc"]
imageURI <- map["imageURI"]
videoURI <- map["videoURI"]
trainerID <- map["trainerID"]
isShoulders <- map["isShoulders"]
isNeck <- map["isNeck"]
isLegs <- map["isLegs"]
isHands <- map["isHands"]
isChest <- map["isChest"]
isCalves <- map["isCalves"]
isButt <- map["isButt"]
isBack <- map["isBack"]
isArms <- map["isArms"]
isCore <- map["isCore"]
isFullBody <- map["isFullBody"]
isLowerBody <- map["isLowerBody"]
isUpperBody <- map["isUpperBody"]
isExercise <- map["isExercise"]
isStretch <- map["isStretch"]
equipmentNeeded <- map["equipmentNeeded"]
现在这是我要映射的 json 示例。请注意,我的模型同时具有“imageURI”和“videoURI”,因为有 2 种类型的媒体对象,但由于它们是可选的,因此可以解释这一点。
"StretchMedia": [
"_id": "5a380db7ee292409b4f44980",
"name": "somephoto",
"desc": "some description",
"imageURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/100006166323",
"trainerID": "5a32f62a28ce4acbc5fbbb4b",
"__v": 0,
"createdAt": "2017-12-18T18:49:27.009Z",
"isShoulders": false,
"isNeck": false,
"isLegs": false,
"isHands": false,
"isChest": false,
"isCalves": false,
"isButt": false,
"isBack": false,
"isArms": false,
"isCore": false,
"isFullBody": false,
"isLowerBody": false,
"isUpperBody": false,
"isExercise": false,
"isStretch": true,
"equipmentNeeded": "none"
,
"_id": "5a380dfa089b2d09dcfa92db",
"name": "somephoto",
"desc": "some description",
"videoURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/100002098677",
"trainerID": "5a32f62a28ce4acbc5fbbb4b",
"__v": 0,
"createdAt": "2017-12-18T18:50:34.277Z",
"isShoulders": false,
"isNeck": false,
"isLegs": false,
"isHands": false,
"isChest": false,
"isCalves": false,
"isButt": false,
"isBack": false,
"isArms": false,
"isCore": false,
"isFullBody": false,
"isLowerBody": false,
"isUpperBody": false,
"isExercise": false,
"isStretch": true,
"equipmentNeeded": "none"
],
"ExerciseMedia": [
"_id": "5a345a356ec5fc0032651611",
"name": "Juan",
"desc": "pic of some Spanish guy",
"imageURI": "https://videofitapptestbucket.s3.us-west-2.amazonaws.com/10000998839",
"trainerID": "5a32f62a28ce4acbc5fbbb4b",
"isStretchCore": false,
"isExerciseCore": false,
"__v": 0,
"createdAt": "2017-12-15T23:26:45.461Z",
"isShoulders": false,
"isNeck": false,
"isLegs": false,
"isHands": false,
"isChest": false,
"isCalves": false,
"isButt": false,
"isBack": false,
"isArms": false,
"isCore": false,
"isFullBody": true,
"isLowerBody": false,
"isUpperBody": false,
"isExercise": true,
"isStretch": false,
"equipmentNeeded": "Dumbbells"
],
我遇到的问题是如何在我的 MediaLibrary 类中将“ExerciseMedia”和“StretchMedia”的内容填充到它们各自的数组中。我创建了一个函数,该函数向包含您刚刚看到的 JSON 的路由发出请求(正文只需要培训师 ID)。到目前为止,我会发布我的代码。
import AlamofireObjectMapper
var mediaLibrary: MediaLibrary?
func setTrainerMediaLibrary(trainerID: String)
let body: [String: Any] = [
"trainerID": trainerID
]
Alamofire.request(TRAINER_MEDIA_ALL, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseObject (response: DataResponse<MediaLibrary>) in
if response.result.error == nil
self.mediaLibrary = Mapper<MediaLibrary>().map(JSONObject: response.result.value)
else
print("Could not map JSON data to Media Library model")
debugPrint(response.result.error as Any)
MediaLibrary 对象返回 nil,所以有些地方不对劲。我的想法是,由于 MediaLibrary 具有“ExerciseMedia”和“StretchMedia”的映射,我需要做的就是在请求中使用 MediaLibrary,映射函数将自动处理您在 JSON 中看到的 2 个媒体键。我可能完全错了,但我知道我很接近,任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:至于AlamofireObjectMapper的用法,对象响应已经映射到json了。您只需调用 .value
即可使用。
self.mediaLibrary = response.result.value
Mapper<MediaLibrary>().map()
的用法是指 ObjectMapper 库本身。
【讨论】:
谢谢!这很简单,但很有帮助以上是关于使用 AlamofireObjectMapper 将对象 json 数据映射到 2 个单独的数组中的主要内容,如果未能解决你的问题,请参考以下文章
使用 AlamofireObjectMapper 映射模型 - swift
AlamofireObjectMapper,嵌套的 JSON 结构在序列化时总是 nil
在 AlamofireObjectMapper 响应中映射 allHeaderFields