1.基本的语法,不要搞错了

2.返回结果的要求

{
    "code": 200,
    "data": {
        "child_id": "ff76b9274947454c919c0d5e71d08e30",
        "stats": {
            "command": {
                "total": 3,
                "categories": [
                    {
                        "category": "我希望他多吃蔬菜,勤洗手,爱刷牙",
                        "count": 3
                    }
                ]
            },
            "哄睡模式": {
                "total": 9,
                "categories": [
                    {
                        "category": "音乐",
                        "count": 3
                    },
                    {
                        "category": "故事",
                        "count": 6
                    }
                ]
            },
            "故事": {
                "total": 10,
                "categories": [
                    {
                        "category": "成语故事",
                        "count": 7
                    },
                    {
                        "category": "快乐运动与游戏乐趣",
                        "count": 2
                    },
                    {
                        "category": "情绪管理与人和相处能力",
                        "count": 1
                    }
                ]
            },
            "音乐": {
                "total": 5,
                "categories": [
                    {
                        "category": "成语故事",
                        "count": 5
                    }
                ]
            }
        }
    }
}

{
    "code": 200,
    "data": {
        "child_id": "ff76b9274947454c919c0d5e71d08e30",
        "stats": [
            {
                "type": "音乐",
                "total": 5,
                "categories": [
                    {
                        "category": "成语故事",
                        "count": 5
                    }
                ]
            },
            {
                "type": "故事",
                "total": 11,
                "categories": [
                    {
                        "category": "成语故事",
                        "count": 8
                    },
                    {
                        "category": "快乐运动与游戏乐趣",
                        "count": 2
                    },
                    {
                        "category": "情绪管理与人和相处能力",
                        "count": 1
                    }
                ]
            },
            {
                "type": "command",
                "total": 3,
                "categories": [
                    {
                        "category": "我希望他多吃蔬菜,勤洗手,爱刷牙",
                        "count": 3
                    }
                ]
            },
            {
                "type": "哄睡模式",
                "total": 10,
                "categories": [
                    {
                        "category": "音乐",
                        "count": 3
                    },
                    {
                        "category": "故事",
                        "count": 7
                    }
                ]
            }
        ]
    }
}

第一个版本的返回要求是:

type StatsResponse struct {
    Code int          `json:"code"`
    Data ResponseData `json:"data"`
}

type ResponseData struct {
    ChildId string              `json:"child_Id"`
    Stats   map[string]StatItem `json:"stats"`
}

type StatItem struct {
    Total      int        `json:"total"`
    Categories []Category `json:"categories"`
}

type Category struct {
    Category string `json:"category"`
    Count    int    `json:"count"`
}

{}就代表着一个对象,而里面的都可以用key,value来表示,所以就用map,而且map不需要切片,一个的{}
就代表着一个map

"stats": { // <-- 这个花括号 { 代表整个“柜子”(一个map)
  "command": { ... }, // <-- 标签和内容的组合,代表一个“抽屉”
  "哄睡模式": { ... }, // <-- 另一个“抽屉”
  "故事": { ... },    // <-- 另一个“抽屉”
  "音乐": { ... }     // <-- 另一个“抽屉”
} // <-- 这个花括号 } 代表“柜子”结束

如果想要map切片,那要求的返回值就得是:

"stats": [ // <-- 最外层是数组 [ ,说明需要切片
  { // <-- 数组里的第一个元素,是一个map(第一个“柜子”)
    "command": { ... },
    "哄睡模式": { ... }
  },
  { // <-- 数组里的第二个元素,是另一个map(第二个“柜子”)
    "儿歌": { ... },
    "古诗": { ... }
  }
]

只需要改变 Stats   []map[string]StatItem `json:"stats"` 就可以了,这里只是演示,实际不会这样
这样复杂化

第二个版本的要求返回是:

type statsResponse struct {
    Code int          `json:"code"`
    Data ResponseData `json:"data"`
}

type ResponseData struct {
    ChildId string `json:"child_Id"`
    Stats   []Item `json:"stats"`
}

type Item struct {
    Type       string     `json:"type"`
    Total      int        `json:"total"`
    Categories []Category `json:"categories"`
}

type Category struct {
    Category string `json:"category"`
    Count    int    `json:"count"`
}

3.JSON格式对应的Go类型