Go map转json

在Go中如何返回前端 字段名称/数量都不确定的json数据?

之前用Go写web服务,返回给前端的json格式的接口,有哪些要返回的字段都是明确的。都是预先定义一个结构体,json.Marshal一下即可~

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

go 复制代码
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))
}

输出为:

go 复制代码
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...](https://link.juejin.cn?target=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F52966444%2Fis-google-protobuf-struct-proto-the-best-way-to-send-dynamic-json-over-grpc "https://stackoverflow.com/questions/52966444/is-google-protobuf-struct-proto-the-best-way-to-send-dynamic-json-over-grpc") "Is "google/protobuf/struct.proto" the best way to send dynamic JSON over GRPC?") **xxxx.proto**: ```go 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** 大致代码如下: ```go var rs xxxxx mapInstance["default"] = mapDefault jsonByteSli, err := json.Marshal(mapInstance) v := &structpb.Value{} err = protojson.Unmarshal(jsonByteSli, v) rs.Data = v return &rs, nil ``` struct.proto源码: [protobuf/src/google/protobuf/struct.proto](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fprotocolbuffers%2Fprotobuf%2Fblob%2Fmain%2Fsrc%2Fgoogle%2Fprotobuf%2Fstruct.proto "https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/struct.proto") [\[转\]Protobuf3 语法指南](https://link.juejin.cn?target=https%3A%2F%2Fcolobu.com%2F2017%2F03%2F16%2FProtobuf3-language-guide%2F "https://colobu.com/2017/03/16/Protobuf3-language-guide/")

相关推荐
源代码•宸15 小时前
Leetcode—3. 无重复字符的最长子串【中等】
经验分享·后端·算法·leetcode·面试·golang·string
0和1的舞者15 小时前
基于Spring的论坛系统-前置知识
java·后端·spring·系统·开发·知识
invicinble16 小时前
对于springboot
java·spring boot·后端
码界奇点17 小时前
基于Spring Boot与Vue的校园后台管理系统设计与实现
vue.js·spring boot·后端·毕业设计·源代码管理
爱编程的小庄17 小时前
Rust 发行版本及工具介绍
开发语言·后端·rust
AI视觉网奇17 小时前
ue 5.5 c++ mqtt 订阅/发布 json
网络·c++·json
Apifox.18 小时前
测试用例越堆越多?用 Apifox 测试套件让自动化回归更易维护
运维·前端·后端·测试工具·单元测试·自动化·测试用例
sunnyday042618 小时前
Nginx与Spring Cloud Gateway QPS统计全攻略
java·spring boot·后端·nginx
康王有点困18 小时前
Link入门
后端·flink
海南java第二人18 小时前
Spring Boot全局异常处理终极指南:打造优雅的API错误响应体系
java·spring boot·后端