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"
]
}
go语言之进阶篇通过map生成json的更多相关文章
- go语言之进阶篇通过结构体生成json
1.通过结构体生成json 示例: package main import ( "encoding/json" "fmt" ) //成员变量名首字母必须大写 t ...
- go语言之进阶篇接口转换
1.go语音之进阶篇 示例: package main import "fmt" type Humaner interface { //子集 sayhi() } type Pers ...
- go语言之进阶篇json解析到map
1.json解析到map(通过类型断言,找到值和value类型) 示例: package main import ( "encoding/json" "fmt" ...
- C#使用Xamarin开发可移植移动应用进阶篇(8.打包生成安卓APK并精简大小),附源码
前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.com/l2999019/DemoApp 可以Star一下,随意 - - 说点什么.. 嗯,前面讲 ...
- go语言之进阶篇JSON处理
一.JSON处理 JSON (JavaScript Object Notation)是一种比XML更轻量级的数据交换格式,在易于人们阅读和编写的同时,也易于程序解析和生成.尽管JSON是JavaScr ...
- Go语言之进阶篇操作redis
1.windows安装redis 软件包下载地址: https://github.com/MicrosoftArchive/redis/releases 1.1.安装--->下一步---> ...
- go语言之进阶篇http客户端编程
1.http客户端编程 示例: http_server.go package main import ( "fmt" "net/http" ) //w, 给客户 ...
- Go语言之进阶篇实现并发聊天功能
1.并发聊天服务器原理分析 2.并发聊天室 功能: 广播消息.广播上线. 查询在线用户.修改用户名.用户主动退出.超时处理 示例: package main import ( "fmt&qu ...
- go语言之进阶篇通过select实现斐波那契数列
一.select作用 Go里面提供了一个关键字select,通过select可以监听channel上的数据流动. select的用法与switch语言非常类似,由select开始一个新的选择块,每个选 ...
随机推荐
- Android 前台服务
Android 前台服务 学习自 https://blog.csdn.net/guolin_blog/article/details/11952435#t3 前台服务漫谈 我们之前学习的Service ...
- Lambda的分类(语句Lambda和表达式Lambda)
学习自 <C#本质论> Overview 在上一文中,我们简而又简的了解了一下,匿名方法和Lambda表达式,关于匿名方法这里暂且不表,本文我们来更加详细的了解一下Lambda表达式. 本 ...
- BZOJ.1018.[SHOI2008]堵塞的交通(线段树维护连通性)
题目链接 只有两行,可能的路径数不多,考虑用线段树维护各种路径的连通性. 每个节点记录luru(left_up->right_up),lurd,ldru,ldrd,luld,rurd,表示这个区 ...
- NOIP练习赛题目2
小K的农场 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个 ...
- hdu 4445 37届金华赛区 D题
题意:给一个坦克的高度,求炮弹能打中最多的数量 枚举角度,作为一名学霸虽然很快推出了公式,但是却没有考虑到,角度可以朝下的情况 #include<cstdio> #include<i ...
- Mysql_解决The total number of locks exceeds the lock table size错误
在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number o ...
- 使用 IntraWeb (11) - 基本控件之 TIWButton
所在单元及继承链: IWCompButton.TIWButton < TIWCustomControl < TIWBaseHTMLControl < TIWBaseControl & ...
- STM32 F4 SPI Accelerometer
STM32 F4 SPI Accelerometer
- github view source
https://insight.io/ http://www.cnplugins.com/devtool/octotree/
- python websocket-client connection
参考:https://pypi.python.org/pypi/websocket-client/ https://www.cnblogs.com/saryli/p/6702260.html i ...