go语言解析 map[string]interface{} 数据格式
原文:https://blog.csdn.net/Nick_666/article/details/79801914
map记得分配内存
解析出来的int类型会变成float64类型
注意判断不为nil后再转换类型
package main
import (
"fmt"
"encoding/json"
)
func main() {
var m map[string]interface{} //声明变量,不分配内存
m = make(map[string]interface{}) //必可不少,分配内存
m["name"] = "simon"
var age int = 12
m["age"] = age
m["addr"] = "China"
print_map(m)
fmt.Println()
data, err:=json.Marshal(m)
fmt.Println("err:", err)
fmt.Println(data)
fmt.Println()
m1 := make(map[string]interface{})
err = json.Unmarshal(data, &m1)
fmt.Println("err:", err)
fmt.Println(m1)
print_map(m1)
fmt.Println()
if m1["name"]!= nil {
fmt.Println(m1["name"].(string))
}
if m1["type"]!= nil {
fmt.Println(m1["type"].(string))
} else {
fmt.Println("there is not the key of type")
}
}
//解析 map[string]interface{} 数据格式
func print_map(m map[string]interface{}) {
for k, v := range m {
switch value := v.(type) {
case nil:
fmt.Println(k, "is nil", "null")
case string:
fmt.Println(k, "is string", value)
case int:
fmt.Println(k, "is int", value)
case float64:
fmt.Println(k, "is float64", value)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range value {
fmt.Println(i, u)
}
case map[string]interface{}:
fmt.Println(k, "is an map:")
print_map(value)
default:
fmt.Println(k, "is unknown type", fmt.Sprintf("%T", v))
}
}
}
运行结果
name is string simon
age is int 12
addr is string China
err: <nil>
[123 34 97 100 100 114 34 58 34 67 104 105 110 97 34 44 34 97 103 101 34 58 49 50 44 34 110 97 109 101 34 58 34 115 105 109 111 110 34 125]
err: <nil>
map[addr:China age:12 name:simon]
name is string simon
addr is string China
age is float64 12
simon
there is not the key of type
Process finished with exit code 0
go语言解析 map[string]interface{} 数据格式的更多相关文章
- GO学习-(38) Go语言结构体转map[string]interface{}的若干方法
结构体转map[string]interface{}的若干方法 本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若 ...
- is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface | mongodb在windows和Linux导出出错
执行mongoexport命令的时候 mongoexport --csv -f externalSeqNum,paymentId --host 127.0.0.1:27017 -d liveX -c ...
- This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa
This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interf ...
- sql.Rows 转换为 []map[string]interface{} 类型
// *sql.Rows 转换为 []map[string]interface{}类型 func rows2maps(rows *sql.Rows) (res []map[string]interfa ...
- map[string]interface{} demo
package main import ( "encoding/json" "fmt" "reflect" ) func demo1() { ...
- c标签遍历List<Map<String, Object>> 数据格式
<c:forEach varStatus="loop" var="dataMap" items="${dataMap}"> &l ...
- 【转】GO语言map类型interface{}转换踩坑小记
原文:https://www.az1314.cn/art/69 ------------------------------------------ mapA := make([string]inte ...
- golang struct 转map 及 map[string]*Struct 初始化和遍历
package main import ( "encoding/json" "errors" "fmt" "reflect&quo ...
- 深度解密Go语言之 map
目录 什么是 map 为什么要用 map map 的底层如何实现 map 内存模型 创建 map 哈希函数 key 定位过程 map 的两种 get 操作 如何进行扩容 map 的遍历 map 的赋值 ...
随机推荐
- Window Batch编程示例
日期时间相关示例 将下面的代码保存为批处理文件 ,命名为GetDate.bat 可以在另外的批处理文件中call GetDate.bat,并直接使用GetDate.bat里面定义的变量,如下图所示: ...
- Python练习-短小精干版三级"片儿"
经过今天Alex大神的指点,终于打通任督二脉了!将昨天比较复杂的代码优化至此:(代码注释后期添加) # 编辑者:闫龙 #三级目录 menu = { '北京':{ '海淀':{ '五道口':{'soho ...
- 【树】ztree
ztree前端参见官网http://www.ztree.me/v3/main.php Action示例 public String initLabServerTree(){ return SUCCES ...
- php 创建验证码方法
php创建验证码方法: <?php function getVerify($length=4,$sessName='verify'){ //验证码 //获取字符串 去除01ol等较难辨认字符 $ ...
- JS设计模式——11.适配器模式
适配器模式概述 适配器模式可用来在现有接口和不兼容的类之间进行适配.使用这种模式的对象又叫包装器(wrapper). 适配器特点 从表面看,适配器模式很像门面模式.她们都要对别的对象进行包装并改变其呈 ...
- python 面试题3
注:本面试题来源于网络. 1.python下多线程的限制以及多进程中传递参数的方式 python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一 ...
- irport报表,把数字金额转换成大写人民币金额
1.编写oracle函数 CREATE OR REPLACE Function MoneyToChinese(Money In Number) Return Varchar2 Is strYuan ) ...
- IE手工导入证书
打开cer文件->欢迎使用证书导入向导->下一步->将所有的证书放入下列存储->受信任的根证书颁发机构->完成
- android studio 解决avd启动问题 ----waiting for target device come online
android studio 模拟器打不开,一直停留在第三方.waiting for target device come online 问题解决方法 方法1.Android Emulator 未 ...
- git —— 异常1,index.lock
git提交过程中出现的问题 解决方法:找到 index.lock文件将其删除 一般 index.lock 在.git下面, 有时 .git 是隐藏的,但是无论怎样, 可以通过 everything 找 ...