-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
42 lines (32 loc) · 844 Bytes
/
main.go
File metadata and controls
42 lines (32 loc) · 844 Bytes
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
package main
import (
"encoding/json"
"fmt"
)
type Book struct {
Title string `json:"title"`
Author Author `json:"author"`
}
type Author struct {
Sales int `json:"book_sales"`
Age int `json:"age"`
Developer bool `json:"is_developer"`
}
type SensorReading struct {
Name string `json:"name"`
Capacity int `json:"capacity"`
Time string `json:"time"`
}
func main() {
author := Author{Sales: 3, Age: 25, Developer: true}
book := Book{Title: "Learning Concurrency in Python", Author: author}
byteArray, err := json.Marshal(book)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(byteArray))
jsonString := `{"name": "battery sensor", "capacity": 40, "time": "2019-01-21T19:07:28Z"}`
var reading SensorReading
err = json.Unmarshal([]byte(jsonString), &reading)
fmt.Printf("%+v\n", reading)
}