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. DBUtil连接数据库

    1. SQL server连接: 数据库不同架包就不同 SQL server 使用的架包是(sqljdbc4.jar) 2. Mysql (MariaDB同理) SQL server 使用的架包是(m ...

  2. [转]SQL server 2008R2 中 C#Winfoirm 使用 SqlDependency 机制实现 数据库中某一张表的监视

    转自:https://blog.csdn.net/u012183487/article/details/77776930 System.Data.SqlClient命名空间下的 sqlDependen ...

  3. MySQL知识篇-SQL1

    1 SQL是什么? 答:是结构话语言,是一种操作关系型数据库的语言. 2 SQL语言分类? SQL语言 说明 举例 DDL 数据定义语言 create  drop DML 数据操作语言 insert ...

  4. 12.Flume的安装

    先把flume包上传并解压 给flume创建一个软链接 给flume配置环境变量 #flume export FLUME_HOME=/opt/modules/flume export PATH=$PA ...

  5. ES-基本概念

    目录: 1.  ElasticSearch是什么 2. 基本概念 3. 几个关键词   正文: 1.  ElasticSearch是什么     ElasticSearch是一个基于Lucene的搜索 ...

  6. Hive_解析 get_json_object ( )

    Hive_解析 get_json_object ( )   get_json_object ( string json_string, string path ) 说明:  第一个参数填写json对象 ...

  7. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  8. JavaScript--QuckStudy

    Day1: 初识JS: https://www.liaoxuefeng.com/wiki/1022910821149312 >打印: alert('我要学JavaScript!'); >J ...

  9. drf-更新四大接口-单改整体-单改局部-群改整体-群改局部-04

    目录 复习 基于前一天序列化基础 整体单改 单与整体局部修改 复习 """ 1.ModelSerializer序列化类 models.py class BaseModel ...

  10. spark内核篇-任务调度机制

    在生产环境中,spark 部署方式一般都是 yarn-cluster 模式,本文针对该模式进行讲解,当然大体思路也适用于其他模式 基础概念 一个 spark 应用包含 job.stage.task 三 ...