英文:
How can I parse array of array to struct in Go?
问题
以下是翻译好的内容:
[
[1501545600000,"a","b","c","d","忽略此项","e",1651363200000],[1504224000000,"a","b","c","d","忽略此项","e",1654041600000],
...
]
我有一堆这样的数组。它来自外部API。我想将其映射到一个结构体中。我不需要"忽略此项"字段。我该如何做?
这是我的结构体:
type Address struct {
RegistrationDate string json:"registrationDate"
Name string json:"name"
Address string json:"address"
City string json:"city"
State string json:"state"
Zip string json:"zip"
ExpirationDate string json:"expirationDate"
}
谢谢。
英文:
[
[1501545600000,"a","b","c","d","pass this","e",1651363200000],[1504224000000,"a","b","c","d","pass this","e",1654041600000],
...
]
I have a bunch of arrays like that. And it came from an external API. I want to map it to a struct. And I don't need the "pass this" field. How can I do this?
And also here is my struct
type Address struct {
RegistrationDate string `json:"registrationDate"`
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
ExpirationDate string `json:"expirationDate"`
}
Thank you.
答案1
得分: 3
你可以让Address
实现json.Unmarshaler
接口。
type Address struct {
RegistrationDate int64 `json:"registrationDate"`
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
ExpirationDate int64 `json:"expirationDate"`
}
func (a *Address) UnmarshalJSON(data []byte) error {
var discard string
return json.Unmarshal(data, &[]interface{}{
&a.RegistrationDate,
&a.Name,
&a.Address,
&a.City,
&a.State,
&discard,
&a.Zip,
&a.ExpirationDate,
})
}
如果你需要日期字段保持为字符串,你可以使用一个"转换器"类型来解析JSON数字,然后将结果的int
转换为string
。
type Int64String string
func (s *Int64String) UnmarshalJSON(data []byte) error {
var i64 int64
if err := json.Unmarshal(data, &i64); err != nil {
return err
}
*s = Int64String(strconv.FormatInt(i64, 10))
return nil
}
type Address struct {
RegistrationDate string `json:"registrationDate"`
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
ExpirationDate string `json:"expirationDate"`
}
func (a *Address) UnmarshalJSON(data []byte) error {
var discard string
return json.Unmarshal(data, &[]interface{}{
(*Int64String)(&a.RegistrationDate),
&a.Name,
&a.Address,
&a.City,
&a.State,
&discard,
&a.Zip,
(*Int64String)(&a.ExpirationDate),
})
}
英文:
You can have Address
implement the json.Unmarshaler
interface.
type Address struct {
RegistrationDate int64 `json:"registrationDate"`
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
ExpirationDate int64 `json:"expirationDate"`
}
func (a *Address) UnmarshalJSON(data []byte) error {
var discard string
return json.Unmarshal(data, &[]any{
&a.RegistrationDate,
&a.Name,
&a.Address,
&a.City,
&a.State,
&discard,
&a.Zip,
&a.ExpirationDate,
})
}
https://go.dev/play/p/eSaXEQ-onOC
If you need the date fields to remain strings you can use a "converter" type to unmarshal the JSON number and then convert the resulting int
to a string
.
type Int64String string
func (s *Int64String) UnmarshalJSON(data []byte) error {
var i64 int64
if err := json.Unmarshal(data, &i64); err != nil {
return err
}
*s = Int64String(strconv.FormatInt(i64, 10))
return nil
}
type Address struct {
RegistrationDate string `json:"registrationDate"`
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
ExpirationDate string `json:"expirationDate"`
}
func (a *Address) UnmarshalJSON(data []byte) error {
var discard string
return json.Unmarshal(data, &[]any{
(*Int64String)(&a.RegistrationDate),
&a.Name,
&a.Address,
&a.City,
&a.State,
&discard,
&a.Zip,
(*Int64String)(&a.ExpirationDate),
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论