使用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. 数据库之_CRUD

    CRUD是指在做计算处理时的增加(Create).读取查询(Retrieve).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中DataBase或者持久层的基本 ...

  2. IFC—IfcProduct实体继承框架

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  4. 更新anaconda及所有包

    ################################## # 更新Anaconda conda update conda # 更新所有包 conda update --all ###### ...

  5. LeetCode第35题:搜索插入位置

    题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6 ...

  6. ObjectARX环境搭建之vs2010+objectArx2012+AutoCAD2012

    ---------------------------------------------------------------------------------------------------- ...

  7. 使用sphinx快速生成Python API 文档

    一  简单介绍 不管是开源还是闭源,文档都是很重要的.当然理论上说,最好的文档就是代码本身,但是要让所有人都能读懂你的代码这太难了.所以我们要写文档.大部分情况,我们不希望维护一份代码再加上一份文档, ...

  8. iOS Programming GitHub

    我把学习<iOS编程(第4版)>的相关代码放在了GitHub上: https://github.com/palanceli/iOSProgramming 学了一段时间之后,当要用到某个知识 ...

  9. ubuntu - 14.04,安装Go语言(谷歌公司开发的一种语言)

    Go语言下载地址:https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz 安装: 1,以root身份在shell里执行: ta ...

  10. [poj 2106] Boolean Expressions 递归

    Description The objective of the program you are going to produce is to evaluate boolean expressions ...