使用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的更多相关文章

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

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

  2. json、map互转

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

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

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

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

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

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

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

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

  7. java json与map互相转换(二)

      java json与map互相转换(二) CreationTime--2018年7月16日15点09分 Author:Marydon 1.准备工作 所需jar包: commons-beanutil ...

  8. java json与map互相转换(一)

      java json与map互相转换(一) CreationTime--2018年7月16日 Author:Marydon 1.准备工作 所需jar包:json-20180130.jar impor ...

  9. GoLang中 json、map、struct 之间的相互转化

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

  10. JavaScript Json与Map互转以及Map对象的取值方式

    Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...

随机推荐

  1. String类型的理解

    引用:https://www.cnblogs.com/binyue/p/3862276.html java语言中: 变量除了八大基本数据类型(byte,short,int,long,boolean,f ...

  2. DBUtils工具类和DBCP连接池

    今日内容介绍 1.DBUtils2.处理结果集的八种方式3.连接池4.连接池的用法1 PrepareStatement接口预编译SQL语句 1.1 预处理对象 * 使用PreparedStatemen ...

  3. 《Maven实战》笔记-2-坐标和依赖

    一.依赖范围 Maven在编译项目主代码的时候,需要使用一套classpath——编译classpath: 在编译和执行测试的时候,使用另一套classpath——测试classpath: 实际运行M ...

  4. DiscreteFrechetDist

    计算离散的frechet 距离,通过计算两条曲线之间的点的距离,将两条曲线上的点按照距离以及曲线的趋势进行配对,最后根据这些配对的距离选出最后的离散frechet距离(compute discrete ...

  5. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  6. ubuntu重新安装 apache2

    $ sudo apt-get --purge remove apache2 $ sudo apt-get --purge remove apache2.2-common $ sudo apt-get ...

  7. 正经学C#_运算符优先级:[c#入门经典]

    学了那么多的运算符,终于差不多结束了,现在要说一下  总体的优先级别 高到低的顺序 类别  运算符  结合性  前缀 ++,--,(),+,-,!,~ 从左到右  乘除  * / %  从左到右  加 ...

  8. Django之博客系统:自定义模板标签

    Django提供了很多内置的模板标签比如{% if %}或者{% block %}Django也允许你创建自己的模板标签(template tags)来执行自定义的动作.当你需要在你的模板中添加功能而 ...

  9. Python3中开发目录的引用

    Python3中开发目录的引用 import os,sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ...

  10. Android - AndroidStudio 的熟悉

    开发环境 * JDK * SDK * AndroidStudio * Genimotioin HelloWorld [ 第一个Android项目建立 ] * 创建项目  [ 项目相关目录 ] Hell ...