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

      

      

  • 相关阅读:
    第一章:帝国的余晖 AT&T公司
    个人最终总结——2(完成了第3个问题)
    个人最终总结——1(完成了前面2个问题)
    week9:个人博客作业
    week8:个人博客作业
    week7:个人博客作业
    week6:个人博客作业
    将博客搬至CSDN
    top命令
    java并发编程的艺术-第四章笔记
  • 原文地址:https://www.cnblogs.com/rsapaper/p/16266320.html
Copyright © 2020-2023  润新知