Go實(shí)現(xiàn)map轉(zhuǎn)json的示例詳解
在Go中如何返回前端 字段名稱/數(shù)量都不確定的json數(shù)據(jù)?
之前用Go寫web服務(wù),返回給前端的json格式的接口,有哪些要返回的字段都是明確的。都是預(yù)先定義一個(gè)結(jié)構(gòu)體,json.Marshal一下即可~
但當(dāng)有的場(chǎng)景,要返回哪些字段不確定時(shí),就無(wú)法使用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來(lái)定義接口,如何定義不確定字段名稱和數(shù)量的返回值?
即上面的 jsonStr,如何定義才能返回給前端?
嘗試使用過(guò)Any,發(fā)現(xiàn)不行(Any的“風(fēng)評(píng)”很不好,介紹時(shí)一般和one of出現(xiàn)在一起)
幾經(jīng)探求,發(fā)現(xiàn)這種情況該用Struct(或說(shuō)Value)類型
[Is "google/protobuf/struct.proto" the best way to send dynamic JSON over GRPC?](stackoverflow.com/questions "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];
// 創(chuàng)建時(shí)間
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, nilstruct.proto源碼: protobuf/src/google/protobuf/struct.proto
到此這篇關(guān)于Go實(shí)現(xiàn)map轉(zhuǎn)json的示例詳解的文章就介紹到這了,更多相關(guān)go map轉(zhuǎn)json內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語(yǔ)言做爬蟲狀態(tài)碼返回418的問(wèn)題解決
在使用Go語(yǔ)言做爬蟲時(shí),使用http.Get(url)去獲取網(wǎng)頁(yè)內(nèi)容,狀態(tài)碼返回404,本文我們就詳細(xì)的介紹一下解決方法,感興趣的可以了解一下2021-12-12
Go語(yǔ)言實(shí)現(xiàn)定時(shí)器的原理及使用詳解
這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言實(shí)現(xiàn)定時(shí)器的兩種方法:一次性定時(shí)器(Timer)和周期性定時(shí)器(Ticker),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12
Go語(yǔ)言中Gin框架使用JWT實(shí)現(xiàn)登錄認(rèn)證的方案
在如今前后端分離開(kāi)發(fā)的大環(huán)境中,我們需要解決一些登陸,后期身份認(rèn)證以及鑒權(quán)相關(guān)的事情,通常的方案就是采用請(qǐng)求頭攜帶token的方式進(jìn)行實(shí)現(xiàn),本文給大家介紹了Go語(yǔ)言中Gin框架使用JWT實(shí)現(xiàn)登錄認(rèn)證的方案,需要的朋友可以參考下2024-11-11
詳解Go并發(fā)編程時(shí)如何避免發(fā)生競(jìng)態(tài)條件和數(shù)據(jù)競(jìng)爭(zhēng)
大家都知道,Go是一種支持并發(fā)編程的編程語(yǔ)言,但并發(fā)編程也是比較復(fù)雜和容易出錯(cuò)的。比如本篇分享的問(wèn)題:競(jìng)態(tài)條件和數(shù)據(jù)競(jìng)爭(zhēng)的問(wèn)題2023-04-04

