go进行JSON和字符串进行互转换

本文介绍了如何使用Go语言的json包处理用户注册请求,包括将结构体转换为JSON字符串,以及从JSON字符串解码回Go对象的过程,以实现数据的编码与验证。
摘要由CSDN通过智能技术生成

以下结构体为例子,请结合自己的进行改造

type RegisterRequest struct {
	Account          string `json:"Account" doc:"用户名称"`
	UserPassword     string `json:"UserPassword" doc:"用户密码"`
	Mobile           string `json:"Mobile" doc:"手机号"`
	Code             string `json:"Code" doc:"短信验证码"`
	VerificationCode string `json:"Code" doc:"风险验证码"`
}

// 转成字符串,req为接收的JSON
accessTokenJSON, _ := json.Marshal(req)
// 解码JSON字符串为Go对象
	var tokenInfo RegisterRequest
	err = json.Unmarshal([]byte(v), &tokenInfo)
	if err != nil {
		fmt.Println("Error decoding JSON:", err)
		return
	}
	// 将Go对象重新编码为JSON字符串
	newAccessTokenJSON, err := json.Marshal(tokenInfo)
	if err != nil {
		fmt.Println("Error encoding JSON:", err)
		return
	}
	fmt.Println(newAccessTokenJSON["Account"].(string))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡忘_cx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值