我可以帮助您进行翻译。以下是需要翻译的内容:
我使用的反序列化结构如下:-
这样做可以进行取消编组,但我无法设置取决于
我需要解析可能具有以下格式的json对象:
格式1:
{
"contactType": 2,
"value": "0123456789"
}
格式2:
{
"contactType": "MobileNumber",
"value": "0123456789"
}
我使用的反序列化结构如下:-
type Contact struct {
ContactType int `json:"contactType"`
Value string `json:"value"`
}
但是这仅适用于格式1。我不想改变ContactType的数据类型,但我想容纳第二种格式。我听说过json.RawMarshal并尝试使用它。
type Contact struct {
ContactType int
Value string `json:"value"`
Type json.RawMessage `json:"contactType"`
}
type StringContact struct {
Type string `json:"contactType"`
}
type IntContact struct {
Type int `json:"contactType"`
}
这样做可以进行取消编组,但我无法设置取决于
json.RawMessage
类型的变量 ContactType
。我该如何设计我的结构以解决这个问题?