0.1.1 json now outputs correctly
This commit is contained in:
21
main.go
21
main.go
@@ -130,7 +130,7 @@ type Data struct {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Fetch data from entur
|
// 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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -195,15 +195,10 @@ func printData(data *Data) {
|
|||||||
fmt.Println(journey.Cancellation)
|
fmt.Println(journey.Cancellation)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a slice to hold the JSON objects
|
// Create a map to hold the JSON object for the current journey
|
||||||
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
|
|
||||||
jsonObject := make(map[string]interface{})
|
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 != "" {
|
if journey.OriginName != "" {
|
||||||
jsonObject["OriginName"] = journey.OriginName
|
jsonObject["OriginName"] = journey.OriginName
|
||||||
}
|
}
|
||||||
@@ -292,17 +287,13 @@ func printData(data *Data) {
|
|||||||
jsonObject["JourneyNote"] = journey.JourneyNote
|
jsonObject["JourneyNote"] = journey.JourneyNote
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonArray = append(jsonArray, jsonObject)
|
// Convert the JSON object to a JSON string
|
||||||
}
|
jsonString, err := json.Marshal(jsonObject)
|
||||||
|
|
||||||
// Convert the JSON array to a JSON string
|
|
||||||
jsonString, err := json.Marshal(jsonArray)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the JSON string
|
// Print the JSON string for the current journey
|
||||||
fmt.Println(string(jsonString))
|
fmt.Println(string(jsonString))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user