Go map转json

今天分享的知识是 Go 接口。如果本文对你有帮助,不妨点个赞,如果你是 Go 语言初学者,不妨点个关注,一起成长一起进步,如果本文有错误的地方,欢迎指出!

但当有的场景,要返回哪些字段不确定时,就无法使用struct的方式。 还可以用map

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
   
	Map2Json()
}

func Map2Json() {
   
	mapInstance := make(map[string]interface{
   })

	mapInstance["Name"] = "cs"
	mapInstance["Age"] = 28
	mapInstance["Address"] = "杭州"

	relation := make(map[string]interface{
   })

	relation["father"] = "cuixxxxxxx"
	relation["mother"] = "yinxxxxx"
	relation["wife"] = "pengxx"

	mapInstance["Relation"] = relation

	pet := make(map[string]interface{
   })
	pet["one"] = "弥弥懵"
	pet["two"] = "黄橙橙"
	pet["three"] = "呆呆"
	pet["four"] = "皮瓜瓜"
	pet["five"] = "斑斑"

	mapInstance["Cat"] = pet

	jsonStr, err := json.Marshal(mapInstance)

	fmt.Println("err is:", err)
	fmt.Println("jsonStr is:", string(jsonStr))
}

输出为:

err is: <nil>
jsonStr is: {
   "Address":"杭州","Age":28,"Cat":{
   "five":"斑斑","four":"皮瓜瓜","one":"弥弥懵","three":"呆呆","two":"黄橙橙"},"Name":"cs","Relation":{
   "father":"cuixxxxxxx","mother":"yinxxxxx","wife":"pengxx"}}

在这里插入图片描述
在proto中如何定义这样的返回值?
如果使用proto来定义接口,如何定义不确定字段名称和数量的返回值?
即上面的 jsonStr,如何定义才能返回给前端?
尝试使用过Any,发现不行(Any的“风评”很不好,介绍时一般和one of出现在一起)
几经探求,发现这种情况该用Struct(或说Value)类型
[Is “google/protobuf/struct.proto” the best way to send dynamic JSON over GRPC?](stackoverflow.com/questions/5… “Is “google/protobuf/struct.proto” the best way to send dynamic JSON over GRPC?”)
xxxx.proto:


syntax = "proto3";
package demo;

import "validate/validate.proto";
import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
//import "google/protobuf/any.proto";
import "google/protobuf/struct.proto";

rpc Getxxxxx(GetxxxxxReq)  returns (GetxxxxxResp) {
   
option (google.api.http) = {
   
	get:"/api/v1/xxxx/xxxx/xxxxxx"
};
}

message GetxxxxxResp {
   
  google.protobuf.Value data = 1;
}

message GetxxxxxReq {
   
  // 用户名
  string user_name = 1
  [(validate.rules).string.max_len = 100, (validate.rules).string.min_len = 1];
  
    // 创建时间
  google.protobuf.Timestamp create_time = 2;

}

xxxx.go 大致代码如下:

ar rs xxxxx
mapInstance["default"] = mapDefault

jsonByteSli, err := json.Marshal(mapInstance)


v := &structpb.Value{
   }

err = protojson.Unmarshal(jsonByteSli, v)

rs.Data = v
return &rs, nil

相关推荐

  1. jsonyolo格式

    2023-12-11 09:04:04       59 阅读
  2. xmljson

    2023-12-11 09:04:04       46 阅读
  3. C# 字符串json

    2023-12-11 09:04:04       32 阅读
  4. jsonexcel

    2023-12-11 09:04:04       34 阅读
  5. 【maskjson】文件互

    2023-12-11 09:04:04       79 阅读
  6. QT : Bson\Json

    2023-12-11 09:04:04       66 阅读
  7. json字符串json对象三种方式

    2023-12-11 09:04:04       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 09:04:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 09:04:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 09:04:04       82 阅读
  4. Python语言-面向对象

    2023-12-11 09:04:04       91 阅读

热门阅读

  1. OkHttp: 使用入门

    2023-12-11 09:04:04       43 阅读
  2. PCIe中断总结-各个中断的区别

    2023-12-11 09:04:04       82 阅读
  3. 什么是漏洞扫描

    2023-12-11 09:04:04       55 阅读
  4. kubebuilder开发operator

    2023-12-11 09:04:04       49 阅读
  5. 说一下一次完整的HTTP请求过程包括哪些内容

    2023-12-11 09:04:04       62 阅读
  6. 220V工频正弦波逆变器设计

    2023-12-11 09:04:04       54 阅读
  7. TP6场景验证问题

    2023-12-11 09:04:04       54 阅读
  8. 数据结构-矩阵

    2023-12-11 09:04:04       55 阅读
  9. 考研真题数据结构

    2023-12-11 09:04:04       80 阅读
  10. 机器学习—混淆矩阵

    2023-12-11 09:04:04       60 阅读
  11. Ubuntu20 USB 权限配置

    2023-12-11 09:04:04       71 阅读