-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
49 lines (41 loc) · 744 Bytes
/
json.go
File metadata and controls
49 lines (41 loc) · 744 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
43
44
45
46
47
48
49
package main
import (
"encoding/json"
"fmt"
)
type Notebook struct {
Marca string
Modelo string
Preco string
}
func main() {
notebooksString := `[
{
"marca":"Acer",
"modelo":"Aspire 5",
"preco":"1234.90"
},
{
"marca":"Acer",
"modelo":"Aspire Nitro 5",
"preco":"3234.90"
},
{
"marca":"Apple",
"modelo":"Macbook air",
"preco":"11234.90"
}
]`
var notebooks []Notebook
jsonString := []byte(notebooksString)
// convertendo json string em struct
json.Unmarshal(jsonString, ¬ebooks)
fmt.Println(notebooks)
// convertendo struct em json
stringJson, err := json.Marshal(notebooks)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(stringJson))
fmt.Printf("%+v\n", notebooks)
}