How to parse JSON Array in Swift using AFNetworking

Here I will describe how to parse a JSON array in Swift using AFNetworking for the request. Suppose your network request returns a JSON like

[
{'prop':'Property1'},
{'prop':'Property2'},
...
]

Here’s the code you want to use

// items
let manager = AFHTTPRequestOperationManager()
        
manager.GET("", parameters: nil,
     success: {
         (operation: AFHTTPRequestOperation!,
         responseObject: AnyObject!) in
         if let jsonArray = responseObject as? NSArray {
                    
              for propInfo in jsonArray {
                 let propObject = propInfo as! NSDictionary
                 let prop:String = propObject["prop"] as! String
                 print("property extracted: \(prop)")                
              }
          }             
      },
      failure: {
          (operation: AFHTTPRequestOperation!, error: NSError!) in
          print("Error: " + error.localizedDescription)
                
      }
)

Leave a Reply

Your email address will not be published. Required fields are marked *