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 ...
随机推荐
- C++面向对象类的实例题目五
题目描述: 编写一个程序,采用一个类求n!,并输出5!的值. 程序代码: #include<iostream> using namespace std; class CFactorial ...
- GCD学习(六) dispatch_async 和dispatch_sync
dispatch_sync(),同步添加操作.他是等待添加进队列里面的操作完成之后再继续执行. dispatch_queue_t concurrentQueue = dispatch_queue_cr ...
- 触摸屏、X11去掉鼠标
cursor disable in X11 Last updated 8 years ago 摘自:http://www.noah.org/wiki/cursor_disable_in_X11 Whe ...
- Person的delete请求--------详细过程
首先,数据库的增删改查都是在PersonRepository中实现,因此,直接进入PersonRepository,找到其父类,搜索delete. @Override @TransactionalMe ...
- Socket编程--基础(基本server/client实现)
IPv4套接口地址结构 IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件中 LINUX结构下的常用结构,一般创建套接字的时候都要将这个结构里面的 ...
- [译]Javascript substring实例
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- 点石成金:访客至上的网页设计秘笈(原书第2版) 中文PDF版
可用性设计是Web设计中最重要也是难度最大的一项任务.本书作者根据多年从业的经验,剖析用户的心理,在用户使用的模式.为扫描进行设计.导航设计.主页布局.可用性测试等方面提出了许多独特的观点,并给出了大 ...
- github基本命令
https://github.com/explore 使用github网站搜索资源 具体搜索 Git教程 - 廖雪峰的官方网站 一. git配置命令 1.设置用户名 git config --glob ...
- 动态合并Repeater控件数据列
前天Insus.NET实现<动态合并GridView数据行DataRow的列>.今天再玩玩Repeater控件,功能也是动态合并某列栏位.Repeater控件跟GridView控件一样集成 ...
- snmp snmp4j的使用
snmp4j的使用 一.什么是snmp及snmp4j? snmp是 Simple Network Management Protocol (简单网络管理协议)的简写. SNMP4J是一个用Java来实 ...