使用对象映射器解析嵌套的字典数组
Parsing nested Array of Dictionaries using Object Mapper
我正在解析网络 api 响应,它是一个字典数组。每个字典依次有一个嵌套的字典数组。我如何解析它?请提供一些代码示例。
我的 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;
}
);
}
);
到目前为止我的代码是,
这是我的整个响应模型 - 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?
Objectmapper 处理嵌套对象,只要它们符合 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"]
}
}
您的第一个模型将代表外部数组。第二个将代表内部数组。这是一个示例
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 创建另一个模型
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"]
}
}
我正在解析网络 api 响应,它是一个字典数组。每个字典依次有一个嵌套的字典数组。我如何解析它?请提供一些代码示例。
我的 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;
}
);
}
);
到目前为止我的代码是,
这是我的整个响应模型 - 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?
Objectmapper 处理嵌套对象,只要它们符合 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"]
}
}
您的第一个模型将代表外部数组。第二个将代表内部数组。这是一个示例
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 创建另一个模型
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"]
}
}