使用Object Mapper解析嵌套的字典数组 [英] Parsing nested Array of Dictionaries using Object Mapper

查看:853
本文介绍了使用Object Mapper解析嵌套的字典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个web api响应,它是一个字典数组。每个字典依次有一个嵌套的字典数组。我该如何解析它? Pl提供了一些代码示例。

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.

我的api响应是,

(
            {
        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,

这是我对整个响应的模型 - ReturnModel

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

截至目前,我正在将RecipientList解析为字典。但我有另一个名为RecipientModel的模型。如何在ReturnModel中使用它来解析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 ?

推荐答案

您的第一个模型将代表外部数组。第二个将代表内部数组。下面是一个示例

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

现在您将为您的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"]
}
}

这篇关于使用Object Mapper解析嵌套的字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆