[go-每日一库]golang Json的编解码

1.Json编码

2.Json解码

代码示例:

package main

import (
	"encoding/json"
	"fmt"
)

func main()  {
	// json -> unmarshal
	raw := `{"one": 2}`
	var dst map[string]int
	_ = json.Unmarshal([]byte(raw), &dst)
	fmt.Printf("after unmarshal, dst data: %v, type: %T\n", dst, dst)

	// marshal -> json
	src := map[string]string{"one": "one"}
	dstByte, _ := json.Marshal(src)
	srcJson := string(dstByte)
	fmt.Printf("src->json: %v, type: %T", srcJson, srcJson)
}

运行结果:

after unmarshal, dst data: map[one:2], type: map[string]int
src->json: {"one":"one"}, type: string

posted on 2022-06-09 16:25  进击的davis  阅读(58)  评论(0编辑  收藏  举报

导航