dtqi87613
2016-10-12 06:54
浏览 641

Golang Bson结构-对JSON中的单个字段使用多个字段名称,但仅将一个字段用于写入数据库

I have a struct like this -

type Address struct {
    AddressLine1 string        `json:"addressLine1" bson:"addressLine1"`
    AddressLine2 string        `json:"addressLine2" bson:"addressLine2"`
    Landmark     string        `json:"landmark" bson:"landmark"`
    Zipcode      string        `json:"zipcode" bson:"zipcode"`
    City         string        `json:"city" bson:"city"`
}

Due to some compatibility issues between the previous build and the latest yet-to-be-released build, I want to make sure that if someone posts json data that decodes using this struct they should be able to use either 'zipcode' or 'pincode' as the field name in their json. But when this value is written to my database, the field name should only be 'zipcode'.

In short,

{
"city": "Mumbai",
"zipcode": "400001"
}

or

{
"city": "Mumbai",
"pincode": "400001"
}

should both appear inside the database as -

{
"city": "Mumbai",
"zipcode": "400001"
}

How do I allow this?

图片转代码服务由CSDN问答提供 功能建议

我有一个像这样的结构-

  type地址结构 {
 AddressLine1字符串`json:“ addressLine1” bson:“ addressLine1”`
 AddressLine2字符串`json:“ addressLine2” bson:“ addressLine2”`
地标字符串`json:“ landmark” bson:“ landmark”`\  n邮政编码字符串`json:“ zipcode” bson:“邮政编码” 
城市字符串`json:“ city” bson:“ city”`
} 
   
 
 

由于以前的版本与最新的尚未发布版本之间存在兼容性问题,因此我想确保,如果有人发布使用此结构解码的json数据,则他们应该能够使用“邮政编码”或“ 密码”作为其json中的字段名称。 但是,当将此值写入我的数据库时,字段名称只能是“邮政编码”。

简而言之,

  {\  n“ city”:“孟买”,
“邮政编码”:“ 400001” 
} 
   
 
 

   {
“ city”:“孟买”,
“ pincode”:“ 400001” 
} 
   
 
 

都应以如下形式出现在数据库中 -

  {
“ city”:“孟买”,
“邮政编码”:“ 400001” 
} 
   
  
 

如何允许这个?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题