Parsing nested Array of Dictionaries using Object Mapper

后端 未结 2 648
北海茫月
北海茫月 2020-11-27 08:55

I am parsing a web api response which is an array of dictionaries. Each dictionary in turn has a nested array of dictionaries. How do i parse it? Pl provide with some code s

相关标签:
2条回答
  • 2020-11-27 09:17

    Objectmapper handles nested objects as long as they conform to Mappable:

        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"]
        }
        }
    
    0 讨论(0)
  • 2020-11-27 09:21

    Your first model will represent outer array. And second will represent inner array. Here is a sample

     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"]
    }
    }
    

    And now you will create another model for your RecipientList

    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"]
    }
    }
    
    0 讨论(0)
提交回复
热议问题