go语言json转map
package util
import (
"encoding/json"
"fmt"
)
// json转map函数,通用
func JSONToMap(str string) map[string]interface{} {
var tempMap map[string]interface{}
err := json.Unmarshal([]byte(str), &tempMap)
if err != nil {
panic(err)
}
return tempMap
}
- 如果map中含有数组,并且数组中是数字,需要求和,如何去做
rain = "{\"reqTime\":\"201911141554\",\"startTime\":\"201911141555\",\"endTime\":\"201911141755\",\"series\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.07,0.14,0.32,0.51,0.72,0.92,1.11,1.28,1.41,1.5,1.53,1.5,1.41,1.28,1.11,0.92,0.72,0.51,0.32,0.14,0.07,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],\"amendNow\":\"多云\",\"amendNowwen\":\"Cloudy\",\"amendNowwcode\":\"01\",\"msg\":\"1小时后会有雪,出门记得带伞\"}"
fmt.Println(rain)
// Json转map, map[string]interface{}
rainMap := util.JSONToMap(rain) //
// 降雨量之和
sum := 0.00
// 遍历map中的series数组,降雨量相加
for _, num := range rainMap["series"].([]interface{}) {
sum += num.(float64) // 这里使用断言
}
fmt.Println(sum) // 结果为 17.490000000000002
如果是多层的json,请参考我的另一篇博客:
(json后的)多层map取值另外感谢网友解决了我的问题:https://www.golangtc.com/t/5628a59fb09ecc56ca000165
go语言json转map的更多相关文章
- go语言json技巧
go语言json技巧 本文总结了在项目中遇到的那些关于go语言JSON数据与结构体之间相互转换的问题及解决办法. 基本的序列化 首先我们来看一下Go语言中json.Marshal()(系列化)与jso ...
- GO语言中json与map的转换
直接上代码(需要引入encoding/json包) // 当前程序的包名 package main // 导入其它的包 import ( "encoding/json" " ...
- json、map互转
首先,json转map 方法一: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 或 Gs ...
- Go语言JSON数据相互转换
目录 结构体转json map转json int转json slice转json json反序列化为结构体 json反序列化为map 结构体转json 结构体转json示例: package main ...
- [转] golang中struct、json、map互相转化
一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age ...
- Go语言基础之map
Go语言基础之map Go语言中提供的映射关系容器为map,其内部使用散列表(hash)实现. map map是一种无序的基于key-value的数据结构,Go语言中的map是引用类型,必须初始化才能 ...
- Go_14:GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)
目前使用的(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)这四种json-map互转,其他的以后在补充.............. ...
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解 一.Jquery遍历筛选数组 1.jquery grep()筛选遍历数组 $().ready( function(){ v ...
随机推荐
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_6-1.常用的第三方支付和聚合支付介绍
笔记 第六章 开发在线教育视频站点核心业务之从零基础接入 微信扫一扫网页支付 1.常用的第三方支付和聚合支付介绍 简介:介绍常用的第三方支付和聚合支付 1.什么是第三方支付 第 ...
- WampServer 下载以及安装问题 以及配置远程连接MYSQL
WampServer 3.0 下载: http://dl.pconline.com.cn/download/52877-1.html 碰到的问题DDL无法添加,解决方法:MSVCR110.DLL fo ...
- nodejs的事件循环1
JavaScript的学习零散而庞杂,因此很多时候我们学到了一些东西,但是却没办法感受到自己的进步,甚至过了不久,就把学到的东西给忘了.为了解决自己的这个困扰,在学习的过程中,我一直试图在寻找一条核心 ...
- 浏览器F12功能键对测试工程师的重要性
F键,功能键,Function键.F12常用于网站界面测试.调试,分析网页所出现的问题,查看html元素.查看响应事件等方面. 打开一个网页,点击F12,弹出一个窗口,其窗口的功能如下: 1 Elem ...
- 开发 --- IDE及小工具
django-debug-toolbar 介绍: django-debug-toolbar 是Django调试工具条,提供了各种信息的获取,拥有极强的调试功能. 官网: https://django- ...
- 【jQuery】attr()、prop()、css() 的区别(转载)
.attr( ) 可以设置元素的属性(也就是给元素新增加一个原来并不存在的属性)也可以获取元素的本来就有的属性以及额外设置的属性.如果要获取的属性没有设置,那么获取到的结果是 undefined; . ...
- web赛题2
@上海赛wp 微信 和 https://www.ctfwp.com/articals/2019unctf.html 后续公告https://unctf.buuoj.cn/notice.html 必看! ...
- pandas - parse-date
1.pd.read_csv()函数中parse_dates()参数 boolean. True -> 解析索引 boolean. If True -> try parsing the in ...
- ZOJ Problem Set - 1003
1.翻译参考 http://www.cnblogs.com/woodfish1988/archive/2006/11/10/556926.html 2.代码参考 http://www.cnblogs. ...
- 生产环境Crontab专业实例
1)书写Crontab定时任务多个基本要领 1.规范定时任务两例 例1:每分钟打印一次自己名字的全拼到 “/server/log/自己命名的文件” 中