ios - 如何使用对象映射器 Swift 映射字典数组
问题描述
嗨,我像这样使用对象映射器编写模型类。
在这里,我得到了 Array of Dictionary 格式的响应。
在这种情况下我们如何映射响应请在下面找到我的代码。
class Modelclass : Mappable{
var mainsite : [Mainsite]!
required init?(map: Map) { }
func mapping(map: Map) {
mainsite <- map["Mainsite"]
}
}
class Mainsite : Mappable{
var categoryId : String!
var cPath : String!
var level : Int!
var name : String!
var parentID : String!
var subItems : [SubItem]!
required init?(map: Map) { }
func mapping(map: Map) {
categoryId <- map["CategoryId"]
cPath <- map["cPath"]
level <- map["level"]
name <- map["Name"]
parentID <- map["ParentID"]
subItems <- map["SubItems"]
}
}
在这里,我尝试使用以下代码映射响应。
func ApiResponse(){
Alamofire.request("https://www.furnitureinfashion.net/menu_category.php", method: .get, parameters: nil, headers: nil).responseJSON { (response) in
if response.data != nil
{
let data = Mapper<Modelclass>().map(JSON: response)
}else{
print(response.error?.localizedDescription ?? "")
}
}
}
解决方案
如果你在这里分享你的回应会容易得多。我假设您正在获取 Mainsite 对象的数组
if response.data != nil
{
let data = Mapper<Mainsite>().map(JSONArray: response)
}
如果你得到的对象是 Mainsite Model 的数组,那么你可以使用 ObjectMapper 的 map JSON Array 方法。它接受两种形式的输入,一种是 AnyObject,另一种是 [[String : Any]],它将返回 Mainsite 模型的数组
推荐阅读
- javascript - 如何检测 React Web 应用程序中的空闲时间?
- azure - 使用 Kusto 查询语言的具有单个数字的图表
- join - 修复连接具有相同变量的两个数据集
- android-asynctask - Android Asynctask 已弃用
- c# - 使用 .NET Core 3.1 搜索网络上的所有 SQL Server 实例
- variables - 特定于 grafana 中的面板的过滤器
- sql - Oracle SQL:根据另一列获取不同列值的计数
- c++ - 尝试使用 ostream<< 运算符对象时与 {fmt} 6.2.1-3 + clang 10.0.0 的链接问题
- python - Django按大小写重新组合
- openstack - 为什么我无法访问 Openstack 元数据服务?