简体   繁体   中英

How to grab data from an API using Swift 4 and Alamofire

I'm trying to get data from an API with no documentation.

My code is

let URL_MAIN = "http://evarsity.srmuniv.ac.in/srmswi/usermanager/youLogin.jsp"
let URL_ATTENDANCE = "http://evarsity.srmuniv.ac.in/srmswi/resource/StudentDetailsResources.jsp?resourceid=7"
let URL_TIMETABLE = "http://evarsity.srmuniv.ac.in/srmswi/resource/StudentDetailsResources.jsp?resourceid=5"

func getData(url: String) {

    Alamofire.request(url, method: .get)
        .responseData { response in
            if response.result.isSuccess {

                print("Sucess! Got the data")

                guard let data = response.result.value else { return }

                print(data)



            } else {
                print("Error: \(String(describing: response.result.error))")

            }
}

I am getting the response as 51406 bytes. I need to get the actual data in either JSON or some other format. Here is the api link for python

https://github.com/arjunmahishi/srm-erp-api/blob/master/erp.py

Convert your responseData to a dictionary using the function i provided below and then parse the data accordingly.Here, im getting a JSON response in the form of a dictionary.

let strResponse = "\(responseString.value!)"
let arr = strResponse.components(separatedBy: "\n")
let dict =  convertStringToDictionary(str:(arr.last  ?? "")!) 
self.Message = dict?["message"] as! String
let responseStatus = dict?["status"] as! NSString


public func convertStringToDictionary(str:String) -> [String: Any]? {
    if let data = str.data(using: .utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
        } catch {
            print(error.localizedDescription)
        }
    }
    return nil
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM