Browse Source

Added json parser

Signed-off-by: Blagovest Petrov <blagovest@petrovs.info>
master
Blagovest Petrov 8 months ago
parent
commit
3dfb755f6e
1 changed files with 32 additions and 9 deletions
  1. +32
    -9
      fetch_data.go

+ 32
- 9
fetch_data.go View File

@ -5,16 +5,19 @@ import (
"fmt"
"io/ioutil"
"net/http"
"crypto/tls"
"encoding/json"
)
type ParkingStatus []struct {
ParkingID int
Time string
FreePlaces string
type ParkingStatus struct {
Parkings []struct {
ParkingID string `json:"code"`
Time string `json:"datetimeChecked"`
FreePlaces string `json:"freePlaces"`
} `json:"parkingRes"`
}
func fetch_data() string {
func fetch_data() []byte {
url := "https://213.240.235.145/web-site-service/rest/parkingBoardsRequest/allFreePlaces"
//because the certificate of the server is self signed
@ -31,14 +34,34 @@ func fetch_data() string {
panic(err)
}
return string(traffic_rawdata)
return traffic_rawdata
}
// func parse_data () string {
func parse_data(data []byte ) ParkingStatus {
var parsed_data ParkingStatus
err := json.Unmarshal(data, &parsed_data)
if err != nil {
fmt.Println(err)
}
// for l := range parsed_data.Parkings {
// fmt.Printf("Parking ID= %v, Time= %v, FreePlaces = %v",
// parsed_data.Parkings[l].ParkingID,
// parsed_data.Parkings[l].Time,
// parsed_data.Parkings[l].FreePlaces )
// fmt.Println()
// }
// }
return parsed_data
}
func main() {
fmt.Printf("%s\n", fetch_data())
fmt.Println()
parse_data(fetch_data() )
}

Loading…
Cancel
Save