如何使用ObjectMapper映射json响应?

时间:2016-08-24 07:38:10

标签: objective-c swift cocoa cocoa-touch mapping

我有像这样的json回复

{   
  frameData = { 
    eStatus = Active;
    frameId = 12;
    "frame_IMEI" = frrrrr;
    "frame_name" = "EHTPKEIWPR_frrrrr";
    "frame_token" = "";
    "frame_user" = 7;
    "is_frameowner" = 1;
    ownerName = jyoti;
    "owner_picture" = "http";
  };
  message = "Frame added successfully.";
  status = 1;
}

我已经创建了这样的模型类

import UIKit
import ObjectMapper

class AddFrameCodeModel: Mappable {

  var status         : Bool?
  var message        : String?
  var frameData      : [FrameDataModel]?


  required init?(_ map: Map) {
    mapping(map)
  }

  func mapping(map: Map) {
    status <- map["status"]
    message <- map["message"]
    frameData <- map["frameData"]

   }

 }

 class FrameDataModel : Mappable {

   var frame_name         : String?
   var eStatus            :String?
   var frame_IMEI     : String?
   var frame_token    : String?
   var frame_user     : String?
   var frameId        : String?
   var is_frameowner  : Bool?
   var ownerName      : String?
   var owner_picture  : String?
   required init?(_ map: Map) {
     mapping(map)
   }

   func mapping(map: Map) {
     frame_name <- map["frame_name"]
     eStatus <- map["eStatus"]
     is_frameowner <- map["is_frameowner"]
     frame_IMEI <- map["frame_IMEI"]
     frame_token <- map["frame_token"]
     frame_user <- map["frame_user"]
     frameId <- map["frameId"]
     ownerName <- map["ownerName"]
     owner_picture <- map["owner_picture"]
   }
 }

但主要的问题是frameData没有映射,因为它的[String : Anyobject]类型,但是当我映射时我必须将它转换为模型类  var frameData : [String : AnyObject]? 它的成功完全映射但它没有转换成模型我必须在模型类中转换它。我的映射代码是

let addFrameCodeData  = Mapper<AddFrameCodeModel>().map(JSONObject)

我的主要问题是frameData也应该被映射,但它不是映射

请给我宝贵的建议。

1 个答案:

答案 0 :(得分:0)

您可以从FrameDataModel类型中删除括号,因为它不是字典响应数组并执行一些&#34;自定义转换&#34;如下https://github.com/Hearst-DD/ObjectMapper

但是我建议你保留JSON给你的类型或者将其中的一些设置为anyObject,然后处理map函数中的所有转换。

我还会为&#34; frameData&#34;创建一个单独的模型。和对待是一个对象。

管理起来更容易,未来可能会发生分离的变化。

试试这个json响应:

import ObjectMapper

class AddFrameCodeResponse:Mappable {

var status         : Int?
var message        : String?
var frameData      : FrameDataResponse?

required init?(_ map: Map){}

func mapping(map: Map)
{
    status <- map["status"]
    message <- map["message"]
    frameData <- map["frameData"]

}

class FrameDataResponse : Mappable{

    var eStatus = String?
    var frameId = Int?
    var frame_IMEI = String?
    var frame_name = String?
    var frame_token = String
    var frame_user = Int?
    var is_frameowner = Int?
    var ownerName = String?
    var owner_picture = String?

    required init?(_ map: Map){}

    func mapping(map: Map)
    {
        eStatus <- map["eStatus"]
        frameId <- map["frameId"]
        frame_IMEI <- map["frame_IMEI"]
        frame_name <- map["frame_name"]
        frame_token <- map["frame_token"]
        frame_user <- map["frame_user"]
        is_frameowner <- map["is_frameowner"]
        ownerName <- map["ownerName"]
        owner_picture <- map["owner_picture"]

    }
}

}

对于您的帧数据模型,请尝试此操作,并根据需要更改类型:

导入基金会

类FrameDataModel {

let eStatus: String
let id: String
let imei: String
let name: String
let token: String
let user: Int
let isOwner: Int
let ownerName: String
let ownerPicture: String

init(eStatus: String, id: String, imei: String, name: String, token: String, user: Int, isOwner: Int, ownerName: String, ownerPicture: String) {
    self.eStatus = eStatus
    self.id = id
    self.imei = imei
    self.name = name
    self.token = token
    self.user = user
    self.isOwner = isOwner
    self.ownerName = ownerName
    self.ownerPicture = ownerPicture
}

}

对于你的地图功能,试试这个:

导入基金会

class JsonResponseToMyClassMapper {     static func map(jsonResponse:AddFrameCodeResponse) - &gt; (status:String,message:String,frameData:FrameDataModel){

    let status : String = jsonResponse.status!

    let message : String = jsonResponse.message!

    //if you changed the types in the frameDataModel, you will need to cast it here
    let frameData : FrameDataModel = FrameDataModel(eStatus: jsonResponse.message!,
                                          id: jsonResponse.frameId!,
                                        imei: jsonResponse.frame_IMEI!,
                                        name: jsonResponse.frame_name!,
                                        token: jsonResponse.frame_token!,
                                        user: jsonResponse.frame_user!,
                                        isOwner: jsonResponse.is_frameowner!,
                                        ownerName: jsonResponse.ownerName!,
                                        ownerPicture: jsonResponse.owner_picture!)

    return (status, message, frameData)

}

}