类似PHP json_encode和json_decode 的用法
代码语言:javascript复制/*
data := map[string]interface{}{
"name": "Tom",
"age": 18,
"gender": "male",
}
str, err := JsonEncode(data)
*/
func JsonEncode(v interface{}) (string, error) {
b, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(b), nil
}
/*
var decodedData map[string]interface{}
err = JsonDecode(str, &decodedData)
*/
func JsonDecode(s string, v interface{}) error {
return json.Unmarshal([]byte(s), v)
}