使用Object Mapperparsing嵌套的字典数组

我parsing一个web api响应,这是一个字典数组。 每个字典都有一个嵌套的字典数组。 我如何parsing它? Pl提供了一些代码示例。

我的答复是,

( { FilingStatusId = 0; FormName = "MISC"; OrderId = 0; RecipientList = ( { FilingStatusId = 0; FormId = 1; FormName = "MISC"; PayerId = 26142; PayerName = bsbbshs; RecipientId = 221438; RecipientName = tests; ReturnId = 209998; UserId = 0; }, { FilingStatusId = 0; FormId = 1; FormName = "MISC"; PayerId = 26142; PayerName = bsbbshs; RecipientId = 221438; RecipientName = tests; ReturnId = 209998; UserId = 0; } ); }, { FilingStatusId = 0; FormName = "MISC"; OrderId = 0; RecipientList = ( { FilingStatusId = 0; FormId = 1; FormName = "MISC"; PayerId = 26142; PayerName = bsbbshs; RecipientId = 221438; RecipientName = tests; ReturnId = 209998; UserId = 0; }, { FilingStatusId = 0; FormId = 1; FormName = "MISC"; PayerId = 26142; PayerName = bsbbshs; RecipientId = 221438; RecipientName = tests; ReturnId = 209998; UserId = 0; } ); } ); 

我的代码到目前为止,

这是我整个响应的模型 – ReturnModel

 import UIKit import ObjectMapper class ReturnModel: Mappable { var FilingStatusId : Int = 0 var FormName : String = "" var OrderId : String = "" var RecipientList:[[String:Any]] = [[:]] required init?(map: Map) { } func mapping(map: Map) { FilingStatusId <- map["FilingStatusId"] FormName <- map["FormName"] OrderId <- map["OrderId"] RecipientList <- map["RecipientList"] } } 

截至目前我parsingRecipientList作为一个字典。 但是我有另一个叫做RecipientModel的模型。 我怎样才能使用它在这个ReturnModelparsingRecipientList?

你的第一个模型将代表外部数组。 第二个将代表内部数组。 这里是一个例子

  import Foundation import ObjectMapper // RecipientModel is an array itself class RecipientModel: Mappable { var filingStatusId:Int var orderId: Int var formName: String var recipientList: [RecipientList] required init?(_ map: Map) { filingStatusId = 0 orderId = 0 formName = "" recipientList = [] } func mapping(map: Map) { filingStatusId <- map["FilingStatusId"] orderId <- map["OrderId"] formName <- map["FormName"] recipientList <- map["RecipientList"] } } 

现在您将为您的RecipientList创build另一个模型

 class RecipientList: Mappable { var filingStatusId:Int var formId: Int var formName: String required init?(_ map: Map) { filingStatusId = 0 formId = 0 formName = "" } func mapping(map: Map) { filingStatusId <- map["FilingStatusId"] formId <- map["FormId"] formName <- map["FormName"] } } 

只要符合Mappable,Objectmapper就处理嵌套对象:

  import UIKit import ObjectMapper class ReturnModel: Mappable { var FilingStatusId : Int = 0 var FormName : String = "" var OrderId : String = "" var RecipientList:[RecipientModel] = [] required init?(map: Map) { } func mapping(map: Map) { FilingStatusId <- map["FilingStatusId"] FormName <- map["FormName"] OrderId <- map["OrderId"] RecipientList <- map["RecipientList"] } }