golang json字符串和map转化

# golang json字符串和map转化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// json 转化为map
func JsonToMap(str string) map[string]interface{} {
	var tempMap map[string]interface{}
	err := json.Unmarshal([]byte(str), &tempMap)
	if err != nil {
		fmt.Println(err)
	}
	return tempMap
}

// json arr 转化为map
func JsonArrToMap(str string) []map[string]interface{} {
	tempMap := make([]map[string]interface{}, 0)
	err := json.Unmarshal([]byte(str), &tempMap)
	if err != nil {
		fmt.Println(err)
	}
	return tempMap
} 

使用举例

1
2
3
4
5
6
json_row := redis_get(redis_key) //
	get_user_token := JsonArrToMap(json_row)
	if user_token == get_user_token[0]["user_token"] {
		//fmt.Println("redis 获取到")
		return true
	}
Licensed under CC BY-NC-SA 4.0
最后更新于 2022-09-07 19:56 UTC
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计