序列化与反序列化 未知结构的数据 Any interface类型
作者:互联网
小结:
1、
type Any interface { } v1 := new(Any)err = json.Unmarshal([]byte(bs1), &v1) V interface{} resp.V = v1
package presentationLayer import ( "TestApp/SRE" service "TestApp/internal/service" "encoding/json" "io" ghttp "github.com/go-kratos/kratos/v2/transport/http" "TestApp/utils" "bytes" "crypto/md5" "fmt" "net/http" "strings" "time" ) func GeoAddress(sreCli *SRE.SREClient, srv *service.GoodsService, ctx ghttp.Context) error { bs, err := io.ReadAll(ctx.Request().Body) if err != nil { sreCli.ApiAlertByQyWxApp(1, bs, nil, ctx.Header()) return err } app_key := "app_key" version := "1.0" timestamp_int := int(time.Now().Unix()) timestamp := utils.Int2Str(timestamp_int) nonce_str := "nonce_str" appSecret := "appSecret" l := []string{ utils.ConcatStr("app_key=", app_key), utils.ConcatStr("nonce_str=", nonce_str), utils.ConcatStr("timestamp=", timestamp), utils.ConcatStr("version=", version), appSecret, } sign := func() string { s1 := strings.Join(l, "&") b := md5.Sum([]byte(s1)) return strings.ToLower(fmt.Sprintf("%x", b)) }() endpoint := "https://api.endpoint/areas/get_list" reader := func() *bytes.Reader { t := map[string]interface{}{ "app_key": app_key, "version": version, "timestamp": timestamp_int, "nonce_str": nonce_str, "sign": sign, } p, _ := json.Marshal(t) return bytes.NewReader(p) }() r, err := http.NewRequest("POST", endpoint, reader) if err != nil { // TODO } r.Header.Set("Content-Type", "application/json") client := &http.Client{} res, err := client.Do(r) if err != nil { // TODO } if res.StatusCode != 200 { } defer res.Body.Close() bs1, err := io.ReadAll(res.Body) if err != nil { // TODO } type Any interface { } v1 := new(Any) err = json.Unmarshal([]byte(bs1), &v1) if err != nil { // TODO } type R struct { CommonResp V interface{} } resp := R{} resp.Code = 0 resp.V = v1 sreCli.ApiAlertByQyWxApp(0, nil, resp, ctx.Header()) return ctx.Result(200, resp) }
{ "Code": 0, "Msg": "", "DevMsg": "", "V": { "code": 200, "data": [ { "area_id": 1, "area_name": "北京市", "children": [ { "area_id": 2, "area_name": "东城区", "children": [ { "area_id": 3, "area_name": "东华门街道", "id": 110101001, "pid": 110101, "status": 1 }, { "area_id": 4, "area_name": "景山街道", "id": 110101002, "pid": 110101, "status": 1 },
标签:nonce,nil,err,area,timestamp,resp,interface,序列化,Any 来源: https://www.cnblogs.com/rsapaper/p/16266320.html