0.1.1 json now outputs correctly

This commit is contained in:
Peder Vatn Austad
2024-05-20 12:48:17 +02:00
parent 1c5c2ae374
commit 0448629ade

21
main.go
View File

@@ -130,7 +130,7 @@ type Data struct {
func main() {
// Fetch data from entur
resp, err := http.Get("https://api.entur.io/realtime/v1/rest/et?maxSize=1")
resp, err := http.Get("https://api.entur.io/realtime/v1/rest/et?maxSize=50")
if err != nil {
log.Fatal(err)
}
@@ -195,15 +195,10 @@ func printData(data *Data) {
fmt.Println(journey.Cancellation)
}
// Create a slice to hold the JSON objects
var jsonArray []map[string]interface{}
// Iterate over the EstimatedVehicleJourney slice
for _, journey := range data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.EstimatedVehicleJourney {
// Create a map to hold the JSON object
// Create a map to hold the JSON object for the current journey
jsonObject := make(map[string]interface{})
// Check if OriginName exists and add it to the JSON object
// Add relevant fields to the JSON object
if journey.OriginName != "" {
jsonObject["OriginName"] = journey.OriginName
}
@@ -292,17 +287,13 @@ func printData(data *Data) {
jsonObject["JourneyNote"] = journey.JourneyNote
}
jsonArray = append(jsonArray, jsonObject)
}
// Convert the JSON array to a JSON string
jsonString, err := json.Marshal(jsonArray)
// Convert the JSON object to a JSON string
jsonString, err := json.Marshal(jsonObject)
if err != nil {
log.Fatal(err)
}
// Print the JSON string
// Print the JSON string for the current journey
fmt.Println(string(jsonString))
}
}