Parsing nested Array of Dictionaries using Object

2019-01-05 06:18发布

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 sample.

My api response is,

(
            {
        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;
            }
        );
    }
);

My code so far is,

This is my model for the entire response - 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"]
}
}

As of now I am parsing the RecipientList as a dictionary. But I have another Model called RecipientModel. How can I use it inside this ReturnModel to parse the RecipientList ?

2条回答
神经病院院长
2楼-- · 2019-01-05 07:05

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"]
    }
    }
查看更多
看我几分像从前
3楼-- · 2019-01-05 07:13

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"]
}
}
查看更多
登录 后发表回答