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 的赋值 ...
随机推荐
- springcloud的Turbine配置监控多个服务的一些坑!!!!InstanceMonitor$MisconfiguredHostException,No message available","path":"/actuator/hystrix.stream,页面不显示服务或者一直loading
踩了几个小时坑,使用仪表盘监控单个服务的时候很容易,但是一到多个服务,瞬间坑就来了,大概碰到下面三个: 1InstanceMonitor$MisconfiguredHostException, No ...
- checkbox 全选和取消
//全选 $("#checkall").click(function () { if (this.checked) { //如果当前点击的多选框被选中 $('input[type= ...
- python3中内建函数map()与reduce()的使用方法
map()的使用 map()的使用方法形如map(f(x),Itera).对,它有两个参数,第一个参数为某个函数,第二个为可迭代对象.如果不懂什么是函数,不懂什么是可迭代对象没关系,记住下面的例 ...
- 如何同步删除svn管理的package包目录
转:https://blog.csdn.net/shiwodecuo/article/details/51754598 eclipse在实际的开发中,当我们的项目由svn进行管理时,若想删除选中的整个 ...
- idea中JDK失效
[问题] 在没有改变任何东西的情况下,突然间IDEA里面所有的代码都标红,无法找到JDK [解决方法] [File]->[Invalidate Caches],然后就好了
- python网络编程--进程线程
一:什么是进程 一个程序执行时的实例被称为一个进程. 每个进程都提供执行程序所需的资源.一个进程有一个虚拟地址空间.可执行代码.对系统对象的开放句柄.一个安全上下文.一个独特的进程标识符.环境变量.一 ...
- Spring框架的基本使用(AOP部分)
AOP,Aspect Oriented Programming,意为面向切面编程,是通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP采取横向抽取机制,取代了传统纵向继承体系重复 ...
- windows系统 安装MongoDB
1.下载 官网下载地址:https://www.mongodb.com/download-center#community 2.配置MongoDB a.在e:\MongoDB(可随意起)下面建一个da ...
- Jackson jsonToBean 忽略 Bean中不存在属性的匹配。
1. jsonToBean时,json中有的字段,bean中没有 无法匹配时,忽略此字段,不抛出异常(默认是抛出异常的) 方案一(局部):在class上加 @JsonIgnoreProperties( ...
- Codeforces 580D Kefa and Dishes(状态压缩DP)
题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x ...