//定义结构体
//首字母大写 , json:"msg_id" 是 tag
type Message struct {
	MsgId   string `json:"msg_id"`
	Content string `json:"content"`
}

//json 序列号反序列化
func T3_1() {
	msg := Message{"msgid_001", "contente2222222222"}
	str, err := json.Marshal(msg)
	//输出 {"msg_id":"msgid_001","content":"contente2222222222"}
	fmt.Println(string(str), err)

	var msg1 Message
	//	str := `{"changes": [{"armid":3,"Index":5}, {"armid":3,"Index":6}]}`
	//反序列化为 stuct
	err = json.Unmarshal(str, &msg1)
	//输出 {msgid_001 contente2222222222}
	fmt.Println(msg1)
	//反序列化为map
	var msg2 map[string]string
	err = json.Unmarshal(str, &msg2)
	//输出 map[msg_id:msgid_001 content:contente2222222222]
	fmt.Println(msg2)
}

  

下面的文章 写的比较详细  

地址:http://www.tuicool.com/articles/zQJFNrf

 

相关文章:

  • 2021-12-10
  • 2022-12-23
  • 2021-05-21
  • 2021-12-25
  • 2022-12-23
  • 2021-08-30
  • 2021-10-05
猜你喜欢
  • 2021-10-19
  • 2021-09-25
  • 2021-07-24
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2021-07-25
相关资源
相似解决方案