Golang json demo code.
To parse of stringify a json, easily use encoding/json
module.
type JsonStruct struct {
Foo int `json:"foo"`
Bar []string `json:"bar"`
}
slcD := []string{"apple", "peach", "pear"}
slcB, _ := json.Marshal(slcD)
fmt.Println(string(slcB))
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB))
jsonStruct := &JsonStruct{
Foo: 1,
Bar: []string{"google", "apple", "microsoft"}}
jsonByte, _ := json.Marshal(jsonStruct)
fmt.Println(jsonByte)
fmt.Println(string(jsonByte))
var jsonString = `
{
"foo": 1,
"bar": ["google", "apple", "microsoft"]
}
`
var newJsonStruct JsonStruct
err := json.Unmarshal([]byte(jsonString), &newJsonStruct)
if err != nil {
log.Fatalln(err)
}
fmt.Println(newJsonStruct)