英文:
list of struct as json golang
问题
我正在尝试返回一个包含每个属性中列表的JSON,但我始终得到空列表。似乎我在结构体中有一个错误,但我找不到它。
我有两个结构体:
type CalendarDay struct {
day int `json:"day"`
weekday string `json:"weekday"`
}
type CalendarYear struct {
January []CalendarDay `json:"january"`
February []CalendarDay `json:"february"`
March []CalendarDay `json:"march"`
April []CalendarDay `json:"april"`
May []CalendarDay `json:"may"`
June []CalendarDay `json:"june"`
July []CalendarDay `json:"july"`
August []CalendarDay `json:"august"`
September []CalendarDay `json:"september"`
October []CalendarDay `json:"october"`
November []CalendarDay `json:"november"`
December []CalendarDay `json:"december"`
}
我想返回的JSON格式如下:
{
"january": [{1 Thursday}, ...],
...
}
但我得到的结果是:
{
"january": [{}, {}, {} ...],
...
}
我的API代码如下:
func Calendar(w http.ResponseWriter, r *http.Request) {
fmt.Println("GET /")
year := getYear(2015)
json.NewEncoder(w).Encode(year)
}
func getMonthDays(month time.Month, year int) []CalendarDay {
cdays := []CalendarDay{}
for i := 1; i <= daysIn(month, year); i++ {
date := time.Date(year, month, i, 0, 0, 0, 0, time.UTC)
weekday := date.Weekday()
cday := CalendarDay{
day: date.Day(),
weekday: weekday.String()}
cdays = append(cdays, cday)
}
return cdays
}
func getYear(year int) CalendarYear {
yearCal := CalendarYear{
January: getMonthDays(time.January, year),
February: getMonthDays(time.February, year),
March: getMonthDays(time.March, year),
April: getMonthDays(time.April, year),
May: getMonthDays(time.May, year),
June: getMonthDays(time.June, year),
July: getMonthDays(time.July, year),
August: getMonthDays(time.August, year),
September: getMonthDays(time.September, year),
October: getMonthDays(time.October, year),
November: getMonthDays(time.November, year),
December: getMonthDays(time.December, year)}
return yearCal
}
我做错了什么?
英文:
I am trying to return a json with a list in each property but I am always obtaining the lists as empty lists. It seems that I have a mistake inside a structure but I cannot found it.
I have two structs:
type CalendarDay struct {
day int `json:"day"`
weekday string `json:"weekday"`
}
type CalendarYear struct {
January []CalendarDay `json:"january"`
February []CalendarDay `json:"february"`
March []CalendarDay `json:"march"`
April []CalendarDay `json:"april"`
May []CalendarDay `json:"may"`
June []CalendarDay `json:"june"`
July []CalendarDay `json:"july"`
August []CalendarDay `json:"august"`
September []CalendarDay `json:"september"`
October []CalendarDay `json:"october"`
November []CalendarDay `json:"november"`
December []CalendarDay `json:"december"`
}
I'm trying to return a json as:
{
"january": [{1 Thursday}, ...]
...
}
but I obatain:
{
"january": [{}, {}, {} ...]
...
}
My API is:
func Calendar(w http.ResponseWriter, r *http.Request) {
fmt.Println("GET /")
year := getYear(2015)
json.NewEncoder(w).Encode(year)
}
func getMonthDays(month time.Month, year int) []CalendarDay {
cdays := []CalendarDay{}
for i := 1; i <= daysIn(month, year); i++ {
date := time.Date(year, month, i, 0, 0, 0, 0, time.UTC)
weekday := date.Weekday()
cday := CalendarDay{
day: date.Day(),
weekday: weekday.String()}
cdays = append(cdays, cday)
}
return cdays
}
func getYear(year int) CalendarYear {
yearCal := CalendarYear{
January: getMonthDays(time.January, year),
February: getMonthDays(time.February, year),
March: getMonthDays(time.March, year),
April: getMonthDays(time.April, year),
May: getMonthDays(time.May, year),
June: getMonthDays(time.June, year),
July: getMonthDays(time.July, year),
August: getMonthDays(time.August, year),
September: getMonthDays(time.September, year),
October: getMonthDays(time.October, year),
November: getMonthDays(time.November, year),
December: getMonthDays(time.December, year)}
return yearCal
}
What am I doing wrong?
答案1
得分: 2
将CalendarDay中的字段导出,通过以大写字符开头的名称。
type CalendarDay struct {
Day int `json:"day"`
Weekday string `json:"weekday"`
}
encoding/json包和类似的包会忽略未导出的字段。
英文:
Export the fields in CalendarDay by starting the name with an uppercase character.
type CalendarDay struct {
Day int `json:"day"`
Weekday string `json:"weekday"`
}
The encoding/json package and similar packages ignore unexported fields.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论