Is it possible to convert this code so as to get JSON from the URL and not just from the file that is in the application

let urlRequest = URLRequest(url: URL(string: "")!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10) let dataTask = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) -> Void in do{ guard error == nil, let responseData = data, let jsonObject = try? JSONSerialization.jsonObject(with: responseData, options: .mutableContainers) as? [String: Any] else { return } //handle object //print(jsonObject) if let postsArray = jsonObject?["data"] as? [[String: AnyObject]] { self.posts = [Post]() for postDictionary in postsArray { let post = Post() post.setValuesForKeys(postDictionary) self.posts.append(post) print("\(post.image2)") } } }catch let err { print(err) } } dataTask.resume() 

If so, please tell me how, but I myself cannot catch up with something. Thank you in advance.

    1 answer 1

    If you just get the data from the GET, then here's a sample for you:

     let urlRequest = URLRequest(url: URL(string: "YOURURL")!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10) let dataTask = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) -> Void in guard error == nil, let responseData = data, let jsonObject = try? JSONSerialization.jsonObject(with: responseData, options: .mutableContainers) else { //handle error if needed return } //handle object print(jsonObject) } dataTask.resume() 
    • I need to get the data and then parse them here in the form I posted, fail to show how to collect it all together - Sergey
    • that something like this should work? It parses everything correctly, but for some reason it gives an error appdelegatelogin [2495: 70413] Unknown class ViewController in Interface Builder file. - Sergey
    • let urlRequest = URLRequest (url: URL (string: "")! cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10) let dataTask = URLSession.shared.dataTask (with: urlRequest) {(data, response, error) -> Void in let responseData = data, let jsonObject = try? JSONSerialization.jsonObject (with: responseData, options: .mutableContainers) as? [String: Any] else {return} if let postsArray = jsonObject? ["Data"] as? [[String: AnyObject]] {self.posts = [Post] () for postDictionary in postsArray {let post = Post () post.setValuesForKeys (postDictionary) self.posts.append (post)}} - Sergey
    • @Sergey Please complete the question with formatted code. - VAndrJ
    • I can not format I can not understand why @VAndrJ - Sergey