如何在Go中将数组的数组解析为结构体?

huangapple go评论91阅读模式
英文:

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),
	})
}

https://go.dev/play/p/Z3TBL82g_lY

huangapple
  • 本文由 发表于 2022年6月11日 20:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/72584278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定