相关推荐recommended
Golang做项目遇到bug invalid character ‘<‘ looking for beginning of value
作者:mmseoamin日期:2023-12-14

1、bug描述

    事先在resis中存入哈希:

HSet users 100 "<\"userId\":100,\"userPwd\":\"123456\",\"userName\":\"scott\">"

创建User结构体,拥有3个字段及对应json tag

type User struct {
	UserId   int    `json:"userId"`
	UserPwd  string `json:"userPwd"`
	UserName string `json:"userName"`
}

通过查询redis得到这个user的哈希value为res,为string类型“<\"userId\":100,\"userPwd\":\"123456\",\"userName\":\"scott\">"

我在把这个字符串转成[]byte后使用json.Unmarshal()来反序列化,把解析后的数据赋给user实例

user = &User{}
err = json.Unmarshal([]byte(res), user)
if err != nil {
		fmt.Println("json.Unmarshal([]byte(res), user)出错 err=", err)
	}

运行后提示json.Unmarshal([]byte(res), user)出错 err= invalid character &#