json转[]byte
paymentData := WxPayData{
ApiKey: __notify.ApiKey,
Appid: __notify.Data.Appid,
MchId: __notify.Data.MchId,
}
paymentDataBuf, _ := json.Marshal(&paymentData)
上图中的paymentData是一个json结构,使用json.Marshal转换完之后的paymentDataBuf是一个[]byte结构
[]byte转json
data := goo.NewHttp().SetUrl(__sns_oauth2_url).Get()
__snsOauth2Response := snsOauth2Response{}
if err := json.Unmarshal(data, &__snsOauth2Response); err != nil {
return nil, err
}
首先定义了一个json结构,如上图中的__snsOauth2Response,他的具体结构如下
type snsOauth2Response struct {
AccessToken string `json:"access_token"`
ExpireIn time.Duration `json:"expire_in"`
Openid string `json:"openid"`
Unionid string `json:"unionid"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
然后用json.Unmarshal把[]byte类型的data转换成json结构的__snsOauth2Response,记得要加指针&
定义的结构除了struct,也可以是map[string]string,或者interface{}
本文介绍了在Go(golang)中如何将json对象转换为[]byte数组,以及如何将[]byte数组解析为json结构。示例中展示了使用json.Marshal和json.Unmarshal进行转换的方法,并提到转换的目标可以是自定义结构体、map[string]string或interface{}。

1081

被折叠的 条评论
为什么被折叠?



