list of struct as json golang

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

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:&quot;day&quot;`
    weekday string `json:&quot;weekday&quot;`
}

type CalendarYear struct {
    January   []CalendarDay `json:&quot;january&quot;`
    February  []CalendarDay `json:&quot;february&quot;`
    March     []CalendarDay `json:&quot;march&quot;`
    April     []CalendarDay `json:&quot;april&quot;`
    May       []CalendarDay `json:&quot;may&quot;`
    June      []CalendarDay `json:&quot;june&quot;`
    July      []CalendarDay `json:&quot;july&quot;`
    August    []CalendarDay `json:&quot;august&quot;`
    September []CalendarDay `json:&quot;september&quot;`
    October   []CalendarDay `json:&quot;october&quot;`
    November  []CalendarDay `json:&quot;november&quot;`
    December  []CalendarDay `json:&quot;december&quot;`
}

I'm trying to return a json as:

{
    &quot;january&quot;: [{1 Thursday}, ...]
    ...
}

but I obatain:

{
    &quot;january&quot;: [{}, {}, {} ...]
    ...
}

My API is:

func Calendar(w http.ResponseWriter, r *http.Request) {
    fmt.Println(&quot;GET /&quot;)
    year := getYear(2015)
    json.NewEncoder(w).Encode(year)
}

func getMonthDays(month time.Month, year int) []CalendarDay {
    cdays := []CalendarDay{}
    for i := 1; i &lt;= 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:&quot;day&quot;`
    Weekday string `json:&quot;weekday&quot;`
}

The encoding/json package and similar packages ignore unexported fields.

huangapple
  • 本文由 发表于 2015年12月14日 01:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/34254345.html
匿名

发表评论

匿名网友

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

确定