1.5 GO json转Map
使用GO将show slave status查询返回的json串转为Map类型
package main import (
"encoding/json"
"fmt"
"strconv"
) /*
以mysql 从库show slave status查询返回的json串为例
查询show slave status以json方式返回
将之转为map[string]interface{}
遍历map转为map[string]string
map[key]返回查询的参数值
*/
func getJSONValue(key string) string {
ss := []byte(`{"Auto_Position":,"Channel_Name":"","Connect_Retry":,"Exec_Master_Log_Pos":,"Executed_Gtid_Set":"","Last_Errno":,"Last_Error":"","Last_IO_Errno":,"Last_IO_Error":"","Last_IO_Error_Timestamp":"","Last_SQL_Errno":,"Last_SQL_Error":"","Last_SQL_Error_Timestamp":"","Master_Bind":"","Master_Host":"10.10.10.10","Master_Info_File":"/data/mysql/data/tanpf/master.info","Master_Log_File":"mysql-bin.000269","Master_Port":,"Master_Retry_Count":,"Master_SSL_Allowed":"No","Master_SSL_CA_File":"","Master_SSL_CA_Path":"","Master_SSL_Cert":"","Master_SSL_Cipher":"","Master_SSL_Crl":"","Master_SSL_Crlpath":"","Master_SSL_Key":"","Master_SSL_Verify_Server_Cert":"No","Master_Server_Id":,"Master_TLS_Version":"","Master_UUID":"5a1c5dfb-b721-11e8-a53e-6c92bf2e0119","Master_User":"db_slave","Read_Master_Log_Pos":,"Relay_Log_File":"mysql-relay-bin.000811","Relay_Log_Pos":,"Relay_Log_Space":,"Relay_Master_Log_File":"mysql-bin.000269","Replicate_Do_DB":"","Replicate_Do_Table":"","Replicate_Ignore_DB":"","Replicate_Ignore_Server_Ids":"","Replicate_Ignore_Table":"","Replicate_Rewrite_DB":"","Replicate_Wild_Do_Table":"","Replicate_Wild_Ignore_Table":"performance_schema.%,information_schema.%,test.%","Retrieved_Gtid_Set":"","SQL_Delay":,"SQL_Remaining_Delay":null,"Seconds_Behind_Master":,"Skip_Counter":,"Slave_IO_Running":"Yes","Slave_IO_State":"Waiting for master to send event","Slave_SQL_Running":"Yes","Slave_SQL_Running_State":"Slave has read all relay log; waiting for more updates","Until_Condition":"None","Until_Log_File":"","Until_Log_Pos":}`)
var f interface{}
err := json.Unmarshal(ss, &f)
if err != nil {
fmt.Println(err)
}
m := f.(map[string]interface{})
mnew := make(map[string]string)
//val := m[key].(string)
res := ""
for k, v := range m {
switch vv := v.(type) {
case string:
//fmt.Println(k, "is string", vv)
res = vv
mnew[k] = res
case int, int8, int16, int32, int64,uint, uint8, uint16, uint32, uint64:
res = fmt.Sprintf("%v",vv)
mnew[k] = res
//fmt.Println(k, "is int", vv)
case float32:
res = strconv.FormatFloat(float64(vv),'f',,)
mnew[k] = res
//fmt.Println(k, "is float32", vv)
case float64:
res = strconv.FormatFloat(vv,'f',,)
mnew[k] = res
//fmt.Println(k, "is float64", res)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
default:
//fmt.Println(k, "is of a type I don't know how to handle")
res = fmt.Sprintf("%v",vv)
mnew[k] = res
}
}
res = mnew[key]
return res
} func main() {
//获取主从延迟
res := getJSONValue("Seconds_Behind_Master")
fmt.Println(res)
}
show slave status其实只用到了string,float64两种类型,有其他类型的可以再对方法进行完善
1.5 GO json转Map的更多相关文章
- GO语言中json与map的转换
直接上代码(需要引入encoding/json包) // 当前程序的包名 package main // 导入其它的包 import ( "encoding/json" " ...
- json、map互转
首先,json转map 方法一: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 或 Gs ...
- [转] golang中struct、json、map互相转化
一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age ...
- 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 ...
- java json与map互相转换(二)
java json与map互相转换(二) CreationTime--2018年7月16日15点09分 Author:Marydon 1.准备工作 所需jar包: commons-beanutil ...
- java json与map互相转换(一)
java json与map互相转换(一) CreationTime--2018年7月16日 Author:Marydon 1.准备工作 所需jar包:json-20180130.jar impor ...
- GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- JavaScript Json与Map互转以及Map对象的取值方式
Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...
随机推荐
- boost::fucntion 用法详解
转载自:http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost ...
- 添加超级链接为什么用a标签
a是anchor的简写,中文意思是锚点,而锚点的引申意思是连接,link已经被html占用了,只能用a来表示连接了.
- JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)
一.结构 二.代码 1. package org.jpwh.model.collections.mapofstrings; import org.jpwh.model.Constants; impor ...
- 100200F Think Positive
传送门 题目大意 给你一个数n和长度为n的序列,序列中的每个数均为1或-1,如果一个点j对于任意的k都满足题目中给的式子,则j是一个合法位置,问这样的j有多少个 分析 这道题有两种方法,分别对应代码1 ...
- then方法
sendSMS(phoneNo).then(function(data) { var msg = (data && data['msg']) ? data['msg'] : '验证码已 ...
- python sort、sorted、reverse、reverd的区别
sort.sorted.reverse.reversed的区别 !!! error 首先应该区分的是,sort和reverse是列表的一个方法.字符串.元组.字典.集合是没有这两个方法的.而sorte ...
- Spark的广播变量模块
有人问我,如果让我设计广播变量该怎么设计,我想了想说,为啥不用zookeeper呢? 对啊,为啥不用zookeeper,也许spark的最初设计哲学就是尽量不使用别的组件,他有自己分布式内存文件系统, ...
- Python pika, TypeError: exchange_declare() got an unexpected keyword argument 'type' 问题修复
网上很多写法都是 type='fanout' 这样的.(这里是基于python=3.6版本, pika=0.13.0 版本) credentials = pika.PlainCredentials(' ...
- 搭建邮件服务器 Postfix + Dovecot (CentOS)
最近分配到一台ECS服务器,需要搭建一台邮件服务器. 查了一波资料选择了Postfix (smtp)和 Dovecot(pop3). 推荐教程:http://www.cnblogs.com/zlbei ...
- Spring.Net 未将对象引用设置到对象的实例
“/”应用程序中的服务器错误. 未将对象引用设置到对象的实例. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详 ...