I am trying to build a public transport app in SwiftUI. I have received an API where I can search for an origin and destination station in Stockholm, Sweden, and the date and time I want to travel and then get a travel route.
This is a part of the JSON file I am working with (the full one is in the comments below):
{ "Trip": [ { "ServiceDays": [ { "planningPeriodBegin": "2021-01-05", "planningPeriodEnd": "2021-05-20", "sDaysR": "m\u00e5n - fre", "sDaysI": "utom 13., 14. maj", "sDaysB": "33E7CF9F3E7CF9F3E7CF9C1E7CF9F3E70F" } ], "LegList": { "Leg": [ { "Origin": { "name": "Skansen", "type": "ST", "id": "A=1@O=Skansen@X=18101848@Y=59323673@U=74@L=400110217@", "extId": "400110217", "lon": 18.101848, "lat": 59.323673, "prognosisType": "PROGNOSED", "time": "15:58:00", "date": "2021-04-06", "rtTime": "15:59:00", "rtDate": "2021-04-06", "hasMainMast": true, "mainMastId": "A=1@O=Skansen (Stockholm)@X=18100895@Y=59323960@U=74@L=300101405@", "mainMastExtId": "300101405", "additional": false }, "Destination": { "name": "Manilla", "type": "ST", "id": "A=1@O=Manilla@X=18133759@Y=59323250@U=74@L=400110141@", "extId": "400110141", "lon": 18.133759, "lat": 59.32325, "prognosisType": "PROGNOSED", "time": "16:05:00", "date": "2021-04-06", "rtTime": "16:06:00", "rtDate": "2021-04-06", "hasMainMast": true, "mainMastId": "A=1@O=Manilla (Stockholm)@X=18134407@Y=59324185@U=74@L=300101426@", "mainMastExtId": "300101426", "additional": false },
for now, I don't focus on changing the date or time, I just need a way to access the following data:
- planningPeriodBegin - planningPeriodEnd - The "Origin" "name" (Skansen) - The "Destination" "name" (Manilla)
I have copied a tutorial on YouTube that partly covers how to access the JSON file - but not completely. Here is the link to the video: https://www.youtube.com/watch?v=-gNOVDNWmIg&list=LL&index=1&t=966s
...And this is the code he wrote in the video:
import SwiftUI struct RouteList: View { @State var gifUrl = String() @State var searchString = String() var body: some View { Text("\(gifUrl)") .onTapGesture { let url = URL(string: gifUrl) guard let GIPHYUrl = url, UIApplication.shared.canOpenURL(GIPHYUrl) else {return} UIApplication.shared.open(GIPHYUrl) } TextField("Search GIFs", text: $searchString) .multilineTextAlignment(.center) Button("Fetch Gif"){fetchAPI()} } func fetchAPI() { let apiKey = "xkkakigCSAnCN4Opymed36OoyByZEbBO" let url = URL(string: "https://api.giphy.com/v1/gifs/search?api_key=\(apiKey)&q=\(self.searchString)&limit=25&offset=0&rating=g&lang=en") URLSession.shared.dataTask(with: url!) { data, response, error in if let data = data { if let decodedGiphy = try? JSONDecoder().decode(GIPHYStructure.self, from: data){ self.gifUrl = decodedGiphy.data[0].url } } }.resume() } } struct GIPHYStructure: Decodable { let data: [dataStructure] } struct dataStructure: Decodable { let url: String }
Thank you for taking the time to read the whole post and it would help greatly if you could manage to figure out how to do it. I have a URL to the JSON file but that key is private.
https://stackoverflow.com/questions/66977198/how-to-access-nested-json-api-data-in-swiftui April 07, 2021 at 06:20AM
没有评论:
发表评论