json转[]bytejson

paymentData := WxPayData{
		ApiKey: __notify.ApiKey,
		Appid:  __notify.Data.Appid,
		MchId:  __notify.Data.MchId,
	}
	paymentDataBuf, _ := json.Marshal(&paymentData)

上图中的paymentData是一个json结构,使用json.Marshal转换完以后的paymentDataBuf是一个[]byte结构url

 

[]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,他的具体结构以下code

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,记得要加指针&token

定义的结构除了struct,也能够是map[string]string,或者interface{}string