How to parse JSON using Alamofire in Swift 3.0 without any third-party library

后端 未结 3 528
粉色の甜心
粉色の甜心 2021-01-15 07:57

Here I want to parse JSON via url. This is what actual JSON data available on url. So I need to parse it and read in my app using Alamofire. But I \'m unable to do it.

3条回答
  •  一整个雨季
    2021-01-15 08:25

     @IBAction func btnget(_ sender: UIButton) {
    
        let urlpath : String = "http://202.131.123.211/UdgamApi_v4/App_Services/UdgamService.asmx/GetAllTeacherData?StudentId=2011111"
    
        let url = URL(string: urlpath)
    
        var urlrequest = URLRequest(url: url!)
    
        urlrequest.httpMethod = "GET"
    
        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)
    
        let task = session.dataTask(with: urlrequest) { (data, response, error) in
    
            do {
                guard let getResponseDic = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject] else {
                    print("error trying to convert data to JSON")
                    return
                }
                // now we have the todo, let's just print it to prove we can access it
    
                print(getResponseDic as NSDictionary)
    
                let dic = getResponseDic as NSDictionary
                let msg = dic.object(forKey: "message")
    
                print(dic)
                //let arrObj = dic.object(forKey: "teacherDetail") as! NSArray
                //print(arrObj)
    
                //let arr = dic.value(forKey: "TeacherName")as! NSArray
    
                print(((dic.value(forKey: "teacherDetail") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "TeacherName") as! String)
    
                // the todo object is a dictionary
                // so we just access the title using the "title" key
                // so check for a title and print it if we have one
    
                // print("The title is: " + todoTitle)
            } catch  {
                print("error trying to convert data to JSON")
                return
            }
        } task.resume()
    }
    

提交回复
热议问题