Hi guys,
since me Firebase works great I need one last step and that’s some JSON Data I have to parse.
I have a JSON which looks like this (“weights” with objects in it) and I need to display every single value of the array Items on its own. It’s hard because there are only tutorials for ListView etc. But I need single variables to display in ContentView.
I got stuck how I have to send the variables from “JSONManager” to “ContentView”
Items.swift look like this:
import Foundation
struct Weights: Codable {
let weights : [Items]
}
struct Items: Codable {
let oew: String
let pax_count: String
let bag_count: String
}
My JSONManager looks like this:
import Foundation
class JSONManager: ObservableObject {
@Published var weights : [Items]init() { fetchRemoteData() } func fetchRemoteData() { let urlString = "https://www.simbrief.com/api/xml.fetcher.php?username=alancord&json=1" let url = URL(string: urlString) let defaultSession = URLSession(configuration: .default) let dataTask = defaultSession.dataTask(with: url!) { data, response, error in if error != nil { print(error!.localizedDescription) return } do { let json = try JSONDecoder().decode(Weights.self, from: data!) DispatchQueue.main.async { self.weights = json.weights } } catch { print(error.localizedDescription) } } dataTask.resume() }
}
ContentView looks like this:
import SwiftUI
struct ContentView: View {
var body: some View { VStack { Text("\(oew)") Text("\(pax_count)") Text("\(bag_count)") } }
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}