简体   繁体   中英

How do i call the parsed data from the GET request that have a longer nested JSON structure in Swift?

NOTE: Forgive my cluelessness, i am still new in regards to this. The full code is posted at the bottom.

ISSUE: It seems that when i have a short nest, i am able to call it for my @Published property however when i try an api request with a longer nest, like this. and type Decodable structs that follows the structure of the GET request

struct TripScheduleTest: Codable {
    let TripList: InitialNest
}
struct InitialNest: Codable {
    var Trip: [TravelDetail]

}
struct TravelDetail: Codable {
    var Leg: [TripTest]
}
struct TripTest: Codable, Hashable {
    var name: String
    var type: String
}

I am not able to call it for the @Published var dataSet1 = [TripTest]()

                self.dataSet1 = tripJSON.TripList.Trip.Leg

I get an error message, that says "Value of type '[TravelDetail]' has no member 'Leg'

I am not sure why, however it works when i use [TravelDetail]() instead of [TripTest]() in the @Published var and stop at Trip before Leg for the dataSet1, then it seems to at least build successfully. But now i am not able to get the name and type information from the request

Full code

import SwiftUI

struct TripScheduleTest: Codable {
    let TripList: InitialNest
}
struct InitialNest: Codable {
    var Trip: [TravelDetail]

}
struct TravelDetail: Codable {
    var Leg: [TripTest]
}
struct TripTest: Codable, Hashable {
    var name: String
    var type: String
}



class TripViewModel: ObservableObject {

    @Published var dataSet1 = [TripTest]()

    init() {
        let urlString = "http://xmlopen.rejseplanen.dk/bin/rest.exe/trip?originId=8600790&destId=6553&format=json"
        guard let url = URL(string: urlString) else { return }
        URLSession.shared.dataTask(with: url) { (data, resp, err) in

            guard let data = data else { return }
            do {
                let tripJSON = try
                    JSONDecoder().decode(TripScheduleTest.self, from: data)
                print(data)
                DispatchQueue.main.async {
                    self.dataSet1 = tripJSON.TripList.Trip.Leg
                }





            } catch {
                print("JSON Decode error: ", error)
            }
        }.resume()
    }
}


struct TripView: View {

@ObservedObject var vm = TripViewModel()

var body: some View {


    List(vm.dataSet1, id: \.self) { day in
        Text("Test")
            .font(.system(size: 12, weight: .bold))
        Text(" \(day.name)")
            .font(.system(size: 12))
    }
}

}

Trip is an array (note the [] )

You need to get one item of the array by index for example

tripJSON.TripList.Trip.first?.Leg

To assign the value to a non-optional array write

self.dataSet1 = tripJSON.TripList.Trip.first?.Leg ?? []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM