How to fetch data from remote JSON in SwiftUI?

so below is the part of the code (in SwiftUI) where local JSON file is used, I want to use remote JSON instead of local, could someone help?

Source is (https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation)

so instead local landmarData.json I want to use https://lkakashv.github.io/landmarkData.json

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

{

let landmarkData: [Landmark] = load("landmarkData.json") 
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)") } }

}

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Thank you

Hello, did you found out how to do it ? I have the same question. Thank you

I have the same question.

You should better start your own thread rather than adding a comment to an unanswered question.
Please do not forget to include your latest code and what you have tried till now, when starting your thread.
Hello
I'll show you what I did

Code Block
class NetworkControllerItalia: ObservableObject {
private var can: AnyCancellable?
let url = URL(string: "apple.com")!
@Published var users = [UserItalia()]
init() {
self.can = URLSession.shared.dataTaskPublisher(for: url)
.map { $0.data }
.decode(type: [UserItalia].self, decoder: JSONDecode
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: {completion in
print(completion)
}, receiveValue: { users in
self.users = users
})
}
}
struct UserItalia: Decodable, Hashable {
}

In the struct you put the properties that you will fetch from your JSON, and instead of apple.com at line 4 you have to put the URL from where you are getting the data, then in the the ContentView you should use a ForEach like this:

Code Block
@ObservedObject var networkController = NetworkControllerItalia()


Code Block
ForEach(networkController.users, id: \.self){ user in
Text(user.nameOfProperty)
}

Have a nice day
How to fetch data from remote JSON in SwiftUI?
 
 
Q