• 序列化与反序列化 未知结构的数据 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
                                },
    

      

      

  • 相关阅读:
    使用ANT入门例子
    React 项目中使用create-react-app创建项目引入antd样式不起作用的问题
    js中对象数组遍历,及区别
    vue中axios封装
    uni-app中开发需要注意事项
    git 提交代码和 合并分支 ,回滚代码
    vue..js3.0
    vue中强制组件重新渲染
    vuex中获取当前路由
    H5 video在移动端播放层级问题
  • 原文地址:https://www.cnblogs.com/rsapaper/p/16266320.html
Copyright © 2020-2023  润新知