将回调Swift JSON AnyObject转换为NSDictionary

时间:2016-11-01 10:53:48

标签: json swift nsdictionary anyobject

我有一个网络连接,使用JSON读取数据并提供回调;

NSDictionary

这叫;

executeRequestURL(requestURL: url, taskCallback: {(status, resp) -> Void in

            if (status == true) {
                if let results = resp as? NSDictionary {
                    print ("\(results.count) results found")
                    let list = results.allValues.first as! NSArray

                    print (list)
                }

            } else {
                print ("Error -- \(resp)")
            }
        })

我遇到的问题是我想将结果读入字典,循环遍历并创建对象。

现在,我将把我的代码放在executeRequestURL中以确保它正常工作,但我打算将这些代码分离出来以备所需的实体。

问题:

如何阅读private class func executeRequestURL(requestURL: NSURL, taskCallback: @escaping (Bool, AnyObject?) -> ()) { print ("Attempting URL -- \(requestURL)") let request: NSURLRequest = NSURLRequest(url: requestURL as URL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: kAPI_TIMEOUT) let session: URLSession = URLSession.shared let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in guard error == nil else { print(error) return } guard let data = data else { print("Data is empty") return } let json = try! JSONSerialization.jsonObject(with: data, options: []) //print(json) if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode { taskCallback(true, json as AnyObject?) } else { taskCallback(false, json as AnyObject?) } }) task.resume() } 字典?

由于

示例响应如下;

resp

3 个答案:

答案 0 :(得分:2)

由于JSON是字典,因此从回调中返回字典([String:Any])。在Swift 3 AnyObject已成为Any。 Swift强大的系统鼓励尽可能具体。

做更好的错误处理!您应该返回错误而不仅仅是false

代码使用新的Swift 3结构URLURLRequest

private class func executeRequestURL(requestURL: URL, taskCallback: @escaping (Bool, [String:Any]?) -> ()) {
  print ("Attempting URL -- \(requestURL)")

  let request = URLRequest(url: requestURL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: kAPI_TIMEOUT)

  let session = URLSession.shared


  let task = session.dataTask(with: request, completionHandler: {
    (data, response, error) in

    guard error == nil else {
      print(error)
      taskCallback(false, nil)
      return
    }
    guard let data = data else {
      print("Data is empty")  // <- this will never be reached. If there is no error,
      taskCallback(false, nil) // data is always non-nil.
      return
    }
    if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
      let json = try! JSONSerialization.jsonObject(with: data, options: []) as!  [String:Any]
      taskCallback(true, json)
    } else {
      taskCallback(false, nil)
    }
  })
  task.resume()
}

JSON结果包含一个字典,其中包含一个键objects,其中包含一个字典数组。 JSON集合类型很容易区分:{}是字典,[]是数组。

要将JSON映射到对象,请创建结构

struct Item {

  var uid : String
  var title : String
  var tempImage : String
  var body : String
  var active : Bool
  var slug : String
  var created : String
  var modified : String
  var endOn : String
}

和一个数组

var items = [Item]()

然后将词典映射到Item

if let objects = json["objects"] as? [[String:Any]] {
  for object in objects {
    let uid = object["uid"] as! String
    var title = object["title"] as! String
    var tempImage = object["temp_image"] as! String
    var body = object["body"] as! String
    var active = object["active"] as! Bool
    var slug = object["slug"] as! String
    var created = object["created"] as! String
    var modified  = object["modified"] as! String
    var endOn  = object["end_on"] as! String
    let item = Item(uid: uid, title: title, tempImage:tempImage, body: body, active: active, slug: slug, created: created, modified: modified, endOn: endOn)
    items.append(item)

  }

JSON值似乎来自一个数据库,该数据库总是包含所有字段,因此强制解包的值是安全的。

答案 1 :(得分:1)

我是这样做的:

func getHttpData(urlAddress : String)
    {
        // Asynchronous Http call to your api url, using NSURLSession:
        guard let url = URL(string: urlAddress) else
        {
            print("Url conversion issue.")
            return
        }
        URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
            // Check if data was received successfully
            if error == nil && data != nil {
                do {
                    // Convert NSData to Dictionary where keys are of type String, and values are of any type
                    let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
                    // Call whatever function you want to do with your dictionary
                    useMyDictionary(dictionary: json)
                } catch {
                    print(error)
                    // Something went wrong
                }
            }
            else if error != nil
            {
                print(error)
            }
        }).resume()
    }

答案 2 :(得分:1)

还有很多其他方法,但我喜欢使用ObjectMapper。它对我来说看起来更干净。所以只需创建一个新的Swift文件,导入ObjectMapper并写下代码。

 class yourDataModel: Mappable {

// MARK: - Constants & Variables

var myObjects: [yourDataModel]

required init?(_ map: Map) {
    myObjects = []

}

func mapping(map: Map) {
    myObjects               <- map["objects"]
}

}

class YourCustomObjects: Mappable {

// MARK: - Constants & Variables

var userId:String
var title:String
var tempimage:String
var body:String
var active:Bool
var slug : String
var createdDate:String
var modifiedDate:String
var endDate:String

// MARK: - init

required init?(_ map: Map) {

    userId = ""
    title = ""
    tempimage = ""
    body = ""
    active = false
    slug = ""
    createdDate = ""
    modifiedDate = ""
    endDate = ""

}

func mapping(map: Map) {

    userId                      <- map["uid"]
    title                       <- map["title"]
    tempimage                   <- map["temp_image"]
    body                        <- map["body"]
    active                      <- map["active"]
    slug                        <- map["slug"]
    createdDate                 <- map["created"]
    modifiedDate                <- map["modified"]
    endDate                     <- map["ends_on"]

}

}

基本上它是你的模型类,现在你只需要将它的结果传递给JSON,希望它是一个AnyObject,它会给你一个包含你所有“对象”的数组。你可以像下面那样使用它

if let data = Mapper<yourDataModel>().map(resp){
    print(data)
 }

试试这个,如果您遇到任何困难,请告诉我。