GO语言中json与map的转换
生活随笔
收集整理的這篇文章主要介紹了
GO语言中json与map的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接上代碼(需要引入encoding/json包)
// 當前程序的包名 package main// 導入其它的包 import ("encoding/json""fmt" )func main() {map2json2map() }func map2json2map() {map1 := make(map[string]interface{})map1["1"] = "hello"map1["2"] = "world"//return []bytestr, err := json.Marshal(map1)if err != nil {fmt.Println(err)}fmt.Println("map to json", string(str))//json([]byte) to mapmap2 := make(map[string]interface{})err = json.Unmarshal(str, &map2)if err != nil {fmt.Println(err)}fmt.Println("json to map ", map2)fmt.Println("The value of key1 is", map2["1"]) }執行代碼?go run jsonmap.go?,結果如下:
??? map to json {"1":"hello","2":"world"}
?? ?json to map? map[1:hello 2:world]
?? ?The value of key1 is hello
?
轉載于:https://www.cnblogs.com/fengxm/p/5997265.html
總結
以上是生活随笔為你收集整理的GO语言中json与map的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用Aspose.Word控件和Aspo
- 下一篇: Java-优秀博客推荐