码迷,mamicode.com
首页 > Web开发 > 详细

go语音之进阶篇通过map生成json

时间:2019-01-14 15:12:03      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:cts   inter   UNC   highlight   执行   company   import   iso   sha   

1、通过map生成json

示例1:

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	//创建一个map
	m := make(map[string]interface{}, 4)
	m["company"] = "itcast"
	m["subjects"] = []string{"Go", "C++", "Python", "Test"}
	m["isok"] = true
	m["price"] = 666.666

	//编码成json
	result, err := json.Marshal(m)
	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	fmt.Println("result = ", string(result))
}

执行结果:

result =  {"company":"itcast","isok":true,"price":666.666,"subjects":["Go","C++","Python","Test"]}

 

示例2:

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	//创建一个map
	m := make(map[string]interface{}, 4)
	m["company"] = "itcast"
	m["subjects"] = []string{"Go", "C++", "Python", "Test"}
	m["isok"] = true
	m["price"] = 666.666

	//编码成json
	result, err := json.MarshalIndent(m, "", "	")
	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	fmt.Println("result = ", string(result))
}

执行结果:

result =  {
	"company": "itcast",
	"isok": true,
	"price": 666.666,
	"subjects": [
		"Go",
		"C++",
		"Python",
		"Test"
	]
}

  

 

go语音之进阶篇通过map生成json

标签:cts   inter   UNC   highlight   执行   company   import   iso   sha   

原文地址:https://www.cnblogs.com/nulige/p/10265839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!