Go map转json

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


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

但当有的场景,要返回哪些字段不确定时,就无法使用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"}}


alt



在proto中如何定义这样的返回值?


如果使用proto来定义接口,如何定义不确定字段名称和数量的返回值?

即上面的 jsonStr,如何定义才能返回给前端?

尝试使用过Any[1],发现不行(Any的“风评”很不好,介绍时一般和one of出现在一起)

几经探求[2],发现这种情况该用Struct[3](或说Value)类型

[Is "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:


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 大致代码如下:


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[4]

[转]Protobuf3 语法指南[5]

参考资料

[1]

Any: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Any

[2]

探求: https://github.com/cuishuang/secret/tree/master/tech/proto

[3]

Struct: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#struct

[4]

protobuf/src/google/protobuf/struct.proto: https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/struct.proto

[5]

[转]Protobuf3 语法指南: https://colobu.com/2017/03/16/Protobuf3-language-guide/

本文由 mdnice 多平台发布

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值