我正在編寫一個 Golang API,當它被調用時,它會從兩個不同的 MongoDB 集合中獲取數據并將其附加到一個結構中,將其轉換為 JSON,然后進行字符串化并發送到 API(Amazon SQS)問題是,定義從 MongoDB 接收的數據的結構,雖然一些字段定義正確,但有些是不同的// IncentiveRule struct defines the structure of Incentive rule from Mongotype IncentiveRule struct { ... Other vars Rule Rule `bson:"rule" json:"rule"` ... Other vars}// Rule defines the struct for Rule Object inside an incentive ruletype Rule struct { ... Rules interface{} `bson:"rules" json:"rules"` RuleFilter RuleFilter `bson:"rule_filter" bson:"rule_filter"` ...}// RuleFilter ...type RuleFilter struct { Condition string `bson:"condition" json:"condition"` Rules []interface{} `bson:"rules" json:"rules"`}雖然這可行,但interface{}內部定義的Rule結構是不同的,并且在獲得 BSON 并解碼和重新編碼為 JSON 時,而不是像JSON 中那樣編碼"fookey":"barvalue",它被編碼為"Key":"fookey","Value":"barvalue",如何避免這種行為并將其作為"fookey":"barvalue"
- 1 回答
- 0 關注
- 260 瀏覽
添加回答
舉報
0/150
提交
取消