/
json.go
37 lines (27 loc) · 802 Bytes
/
json.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
// Description: JSON marshalling and unmarshalling
// Tags: json, json marshal, json marshal, marshal, marshal json, marshal json, json, json marshal, json marshal, marshal, marshal json, marshal json
package main
import (
"encoding/json"
"fmt"
"os"
)
//var strings = []string{"lorem", "ipsum", "dolor", "sit", "amet"}
func main() {
var strings []string
var jsonstring = `["lorem", "ipsum", "dolor", "sit", "amet"]`
//convert json bytes-string to object
err := json.Unmarshal([]byte(jsonstring), &strings)
if err != nil {
fmt.Println("error while unmarshalling")
os.Exit(2)
}
fmt.Println(strings)
//convert object to bytes-string
jsonData, err := json.Marshal(strings)
if err != nil {
fmt.Println("error while marshalling")
os.Exit(2)
}
fmt.Println(string(jsonData))
}