-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_test.go
54 lines (45 loc) · 1.33 KB
/
json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Programs struct {
Programs []Program `json:"programs"`
}
type Program struct {
Name string `json:"name"`
FullName string `json:"fullName"`
Description string `json:"description"`
URL string `json:"url"`
Alternatives []string `json:"alternatives"`
}
func main() {
// Open jsonFile
jsonFile, err := os.Open("programs.json")
// handle error os.Open may return
if err != nil {
fmt.Println(err)
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read opened jsonFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
//initialize programs array
var programs Programs
// unmarshal the byteArray from jsonFile
json.Unmarshal(byteValue, &programs)
// we iterate through every program within our programs array and
// print out some attributes
for i := 0; i < len(programs.Programs); i++ {
fmt.Println("Name: " + programs.Programs[i].Name)
fmt.Println("Full Name: " + programs.Programs[i].FullName)
fmt.Println("Description: " + programs.Programs[i].Description)
fmt.Println("URL: " + programs.Programs[i].URL)
for j := 0; j < len(programs.Programs[i].Alternatives); j++ {
fmt.Println("Alternatives: " + programs.Programs[i].Alternatives[j])
}
fmt.Println("---")
}
}