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

go语言json转map的更多相关文章

  1. go语言json技巧

    go语言json技巧 本文总结了在项目中遇到的那些关于go语言JSON数据与结构体之间相互转换的问题及解决办法. 基本的序列化 首先我们来看一下Go语言中json.Marshal()(系列化)与jso ...

  2. GO语言中json与map的转换

    直接上代码(需要引入encoding/json包) // 当前程序的包名 package main // 导入其它的包 import ( "encoding/json" " ...

  3. json、map互转

    首先,json转map 方法一: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 或 Gs ...

  4. Go语言JSON数据相互转换

    目录 结构体转json map转json int转json slice转json json反序列化为结构体 json反序列化为map 结构体转json 结构体转json示例: package main ...

  5. [转] golang中struct、json、map互相转化

    一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age ...

  6. Go语言基础之map

    Go语言基础之map Go语言中提供的映射关系容器为map,其内部使用散列表(hash)实现. map map是一种无序的基于key-value的数据结构,Go语言中的map是引用类型,必须初始化才能 ...

  7. Go_14:GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  8. 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互转,其他的以后在补充.............. ...

  9. Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解

    Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解 一.Jquery遍历筛选数组 1.jquery grep()筛选遍历数组 $().ready( function(){ v ...

随机推荐

  1. [PySpark] RDD programming on a large file

    重难点 一.parallelize 方法 一般来说,Spark会尝试根据集群的状况,来自动设定slices的数目.然而,你也可以通过传递给parallelize的第二个参数来进行手动设置. data_ ...

  2. baidu echats简介

    http://note.youdao.com/noteshare?id=ef467acdbe5b84005a1315d77a4475d1

  3. Linux学习—redis安装配置及远程连接

    1.下载安装包并解压 进入文件夹/usr/local cd /usr/local 下载redis安装包: wget http://download.redis.io/releases/redis-.t ...

  4. 转:获取windows凭证管理器明文密码

    1.运行cmdkey /list查看windows保存凭证 方法1.mimikaz mimikatz vault::cred 2.利用powershell尝试获取 windows 普通凭据类型中的明文 ...

  5. Hbase和Hadoop的内存参数调优 + 前端控制台

    1.hadoop的内存配置调优 mapred-site.xml的内存调整 <property> <name>mapreduce.map.memory.mb</name&g ...

  6. Centos7源码安装Apache和PHP

    源码安装Apache 安装需要的依赖 yum -y install gcc autoconf automake make pcre pcre-devel openssl openssl-devel​# ...

  7. [bzoj3162]独钓寒江雪_树hash_树形dp

    独钓寒江雪 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3162 题解: 首先,如果没有那个本质相同的限制这就是个傻逼题. 直接树形dp ...

  8. 网站循环加载监控-C#

    背景: 公司有一个报表的网站,服务器或系统不太稳定,导致客户有时候查看报表网址的时候网站打不开或者打开时间过长,影响用户体验 需求: 通过程序循环打开网址了解加载情况,使用谷歌浏览器内核.,程序开发不 ...

  9. STL pair 常见用法详解

    <算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...

  10. Linux就该这么学——新手必须掌握的命令之打包压缩与搜索命令组

    tar命令 用途 : 对文件进行打包或者解压 格式 : tar [选项] [文件] 表 tar命令的参数及作用 参数 作用 -c 创建压缩文件 -x 解开压缩文件 -t 查看压缩包内有哪些文件 -z ...