Mutual conversion between golang map and string

Map and json convert each other

MapToJson is transferred from map to json JsonToMap is json is map

package main
import (
func MapToJson(param map[string]interface{}) string {
    dataType, _ := json.Marshal(param)
    dataString := string(dataType)
    return dataString
func JsonToMap(str string) map[string]interface{} {
    //map 转json
    var tempMap map[string]interface{}
    err := json.Unmarshal([]byte(str), &tempMap)
    if err != nil {
    return tempMap
func main() {
    params := make(map[string]interface{})
    params["name"] = "test"
    params["domain"] = ""
    parmJson := MapToJson(params)
    fmt.Println("parmJson:", parmJson)
    parmMap := JsonToMap(parmJson)
    fmt.Println("parmMap:", parmMap)

Leave a Reply

Your email address will not be published. Required fields are marked *