json - 如何在 Go 中解组 Json 数组
问题描述
我正在努力解析以下 json 数据:
[{"id":90129966,"from":"user@admin.com","subject":"golang test","date":"2020-10-20 07:39:55"}]
这是我的代码:
package main
import (
"encoding/json"
"fmt"
)
type JsonTemplate struct {
Id int `json:"id"`
From string `json:"from"`
Subject string `json:"subject"`
Date string `json:"date"`
}
type Response struct {
JsonTemplate []JsonTemplate
}
func main() {
mockJson := `[{"id":90129966,"from":"user@admin.com","subject":"golang test","date":"2020-10-20 07:39:55"}]`
var response Response
err := json.Unmarshal([]byte(mockJson), &response)
if err != nil {
fmt.Println(err)
}
fmt.Println(response)
}
输出:
json: cannot unmarshal array into Go value of type main.Response
{[]}
我不知道我在这里做错了什么。有人能指出我正确的方向吗?
解决方案
从 Flimzy 那里得到了答案:var response []JsonTemplate
。再次感谢。
推荐阅读
- service - 如何在 Symfony 4 中将供应商类加载为服务?
- excel - Excel CustomUI 功能区放置在另一个 CustomUI 功能区选项卡旁边
- c# - 在 SharePoint 中创建一个继承自分类(托管元数据)字段的自定义字段
- google-cloud-platform - 为公共 GKE 集群设置 Cloud NAT
- c# - 如何在 C# 中的 Combo Box 中隐藏或显示 false 项目?
- c - 读取和打印宽字符串
- java - Java中24点后每天调用方法的逻辑
- docker - Docker:OCI 运行时创建失败:$PATH 中找不到可执行文件
- android - 如何在小部件的远程视图中制作垂直可滚动的文本视图?
- vb.net - 让多个 ComboBox 使用相同的事件