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 ...
随机推荐
- css 层叠式样式表(2)
一,样式表分类 (1)内联样式. --优先级最高,代码重复使用最差. (当特殊的样式需要应用到单独某个元素时,可以使用. 直接在相关的标签中使用样式属性.样式属性可以包含任何 CSS 属性.) (2) ...
- JS对表单的操作
JS对表单中的style的操作,包括复选框技术 废话不多说直接上文件代码!!! 功能:全选\反选,鼠标监测变颜色 <html> <head> <meta charset= ...
- ROS Learning-012 beginner_Tutorials (编程) 创建自定义的ROS消息和ROS服务
ROS Indigo beginner_Tutorials-11 创建自定义的ROS消息和ROS服务 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubunt ...
- Be a Smart Raftsman SGU475
传送门 题目大意 有m+1个点,0是起点,m是终点,i-1到i有一条边,有一个船由0驶往m,不能返回,它在载重小于等于ci时通过第i条边消耗的时间为di否则为Di,现在有n个人,每个人体重为wi,上船 ...
- WOJ 41 约数统计
只会写60分算法QuQ 考虑到一个数$x$大于$\sqrt{x}$的质因数最多只有一个,我们可以筛出小于$\sqrt{r}$范围内的所有质因数然后直接用这些取分解质因数. 最后扫一遍发现还没有分解完的 ...
- 10.model/view实例(3)
任务:3x2的表格,第一个单元格显示当前时间 思考: 1.data函数里面QTime::currentTime()显示当前时间 2.但是这个事件是一个固定的时间,不会变动 3.需要时间变动,View就 ...
- AbstractBootstrap的研读
AbstractBootstrap是一个工具类,用来配置和启动Channel的,下面看下AbstractBootstrap的类继承,ServerBootstrap用于启动ServerChannel的, ...
- java的import关键字的使用
在java中如何使用Java包中自带的类呢? 方法一: 在使用时可以用Java.(包名).(方法名).(包中的类名): 例如:Java.util.Arrays.toString(某个要排序数组); 具 ...
- C++轮子队-第六周--事后分析
C++轮子队 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 实现2048+俄罗斯方块结合的小游戏,定义的比较清楚,典型用户也很清晰,提供给那些对该类游 ...
- 在FooterTemplate内显示DropDownList控件
如果想在Gridview控件FooterTemplate内显示DropDownList控件供用户添加数据时所应用.有两种方法可以实现,一种是在GridView控件的OnRowDataBound事件中写 ...