Take json data from url

Hi! I would like to take in json data from a url that I already have set up, using something like this. Thanks!



func load<T: Decodable>(_ filename: String) -> T {

    let data: Data



    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)

        else {

            fatalError("Couldn't find \(filename) in main bundle.")

    }



    do {

        data = try Data(contentsOf: file)

    } catch {

        fatalError("Couldn't load \(filename) from main bundle:\n\(error)")

    }



    do {

        let decoder = JSONDecoder()

        return try decoder.decode(T.self, from: data)

    } catch {

        fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")

    }

}

What do you get ? What did you expect ? In a word, what is the problem ?

Take json data from url
 
 
Q