[英]How to get data as is from MongoDB and send it to an API as JSON in Golang
I am writing a Golang API at work which when called, gets data from two different MongoDB Collections and appends it into a struct, converts it to JSON, and stringified and sends to an API (Amazon SQS)
问题是,定义从 MongoDB 接收的数据的结构,虽然有些字段定义正确,但有些是变化的
// IncentiveRule struct defines the structure of Incentive rule from Mongo
type IncentiveRule struct {
... Other vars
Rule Rule `bson:"rule" json:"rule"`
... Other vars
}
// Rule defines the struct for Rule Object inside an incentive rule
type 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"`
}
虽然这可行,但在Rule
结构中定义的interface{}
是变化的,并且在获得 BSON 并解码和重新编码为 JSON 时,而不是在 JSON 中编码为"fookey":"barvalue"
,它被编码为"Key":"fookey","Value":"barvalue"
,如何避免这种行为并将其设置为"fookey":"barvalue"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.