CAN't PARSE THE JSON

struct JSONData: Decodable {

    var data: [APIResponse]?

 }

struct APIResponse: Codable {

    var title: String?

    var created_at: String?

}

class ViewController: UIViewController {

override func viewDidLoad() {

    super.viewDidLoad()

    let jsonUrlString = "https://api.github.com/repos/Alamofire/Alamofire/issues"

    guard let url = URL(string: jsonUrlString) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, err) in

        guard let data =  data else{ return }

        do {

            let courses = try JSONDecoder().decode(JSONData.self, from: data)

            print(courses.data as Any)

        } catch let jsonErr {

            print("Error serializing json:", jsonErr)

        }

    }.resume()

   }

}

The error message is correct. The response is an array of objects compatible with your APIResponse structure, but your code tries to decode it as a JSONData structure. So the decoder is expecting to find a top-level JSON object with a field named data, which of course doesn’t exist in the response.

The correct type to pass to the decoder is [APIResponse].self.

CAN't PARSE THE JSON
 
 
Q