iOS - 使用swift中的ObjectMapper映射根JSON数组

Nic*_*AUX 36 json nsarray ios swift

我使用库ObjectMapper将json映射到我的对象,但是我有一些问题要映射根json数组.

这是收到的json:

[
   {
       CustomerId = "A000015",
       ...
   },
   {
       CustomerId = "A000016",
       ...
   },
   {
       CustomerId = "A000017",
       ...
   }
]
Run Code Online (Sandbox Code Playgroud)

这是我的目标

class Customer : Mappable
{
    var CustomerId : String? = nil

    class func newInstance(map: Map) -> Mappable? {
        return Customer()
    }

    func mapping(map: Map) {
        CustomerId   <- map["CustomerId"]
    }
}
Run Code Online (Sandbox Code Playgroud)

我用我的控制器映射json

let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSArray

if (error != nil) {
    return completionHandler(nil, error)
} else {
    var customers = Mapper<Customer>().map(json)
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我试过Mapper<[Customer]>().map(json)但它也不起作用.最后,我尝试创建一个包含Customer数组的新swift对象CustomerList,但它不起作用.

你知道如何映射根数组的json吗?

谢谢.

Nic*_*AUX 58

我终于解决了我的问题:

控制器中的映射方法应该是

let json : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error)

if (error != nil) {
    return completionHandler(nil, error)
} else {
    var customer = Mapper<Customer>().mapArray(json)! //Swift 2
    var customer = Mapper<Customer>().mapArray(JSONArray: json)! //Swift 3
}
Run Code Online (Sandbox Code Playgroud)

如果它可以帮助某人.


Ima*_*tit 9

使用JSONObjectWithData(::)正确的条件向下转换类型

你的JSON是类型的[[String: AnyObject]].因此,使用Swift 2,您可以使用JSONObjectWithData(::)条件向下转换类型[[String: AnyObject]]以防止使用NSArrayAnyObject!:

do {
    if let jsonArray = try NSJSONSerialization
        .JSONObjectWithData(data, options: []) as? [[String: AnyObject]] {
        /* perform your ObjectMapper's mapping operation here */
    } else {
        /* ... */
    }
}
catch let error as NSError {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)

映射到Customer使用mapArray(:)方法

ObjectMapperMapper类提供了一个调用的方法mapArray(:)有以下声明:

public func mapArray(JSONArray: [[String : AnyObject]]) -> [N]?

ObjectMapper关于它的文档指出:

将JSON字典数组映射到Mappable对象数组

因此,您的最终代码应如下所示:

do {
    if let jsonArray = try NSJSONSerialization
        .JSONObjectWithData(data, options: []) as? [[String: AnyObject]] {
        let customerArray = Mapper<Customer>().mapArray(jsonArray)
        print(customerArray) // customerArray is of type [Customer]?
    } else {
        /* ... */
    }
}
catch let error as NSError {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)

映射到Customer使用map(:)方法

ObjectMapperMapper类提供了一个调用的方法map(:)有以下声明:

func map(JSONDictionary: [String : AnyObject]) -> N?

ObjectMapper关于它的文档指出:

将JSON字典映射到符合Mappable的对象

作为前面代码的替代,以下代码显示了如何将JSON映射到Customer使用map(:):

do {
    if let jsonArray = try NSJSONSerialization
        .JSONObjectWithData(data, options: []) as? [[String: AnyObject]] {
        for element in jsonArray {
            let customer = Mapper<Customer>().map(element)
            print(customer) // customer is of type Customer?
        }
    } else {
        /* ... */
    }
}
catch let error as NSError {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)