zoukankan      html  css  js  c++  java
  • go语言之进阶篇通过map生成json

    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"
    	]
    }
    

      

  • 相关阅读:
    不同用户表的导入导出
    视图合并和谓词推入
    pgsql 的函数
    pgsql_pg的数据类型
    个人最终总结
    结对编程黄金点游戏
    阅读代码
    Visual Studio2015安装过程以及单元测试
    软件工程(2018)第五次团队作业
    软件工程(2018)第二次团队作业
  • 原文地址:https://www.cnblogs.com/nulige/p/10265839.html
Copyright © 2011-2022 走看看