go - 如何将结构保存为扁平化的 json?
问题描述
type Profile struct {
Personal struct {
Age int `json:"age"`
Address string `json:"address"`
} `json:"personal"`
Education struct {
Bachelor string `json:"bs"`
Master string `json:"ms"`
} `json:"education"`
}
json.Marshal
产生类似的json
{
"personal": { "age": 40, "address": "ABC" },
"education" : { "bs": "AAA", "ms" :"BBB"}
}
我怎样才能创建一个扁平的json,比如
{
"personal.age" : 40, "personal.address",
"education.bs" : "AAA", "education.ms" : "BBB"
}
我搜索了一些帖子,但除了展平地图外找不到任何其他内容。
解决方案
最简单的方法(使用encoding/json
):
type Personal struct {
Age int `json:"personal.age"`
Address string `json:"personal.address"`
}
type Education struct {
Bachelor string `json:"education.bs"`
Master string `json:"education.ms"`
}
type Profile struct {
Personal
Education
}
但是您可以看到,将 struct 标签更改为person.age
.
推荐阅读
- c# - 如何通过 Unity 使用 WebGL/C# 在新选项卡上打开链接?
- discord.js - 兑换命令 (discord.js)
- python-3.x - 我目前正在尝试创建一个基于变量命名的字典
- amazon-web-services - S3 是否提供任何存储桶策略以在有限的时间内将对象共享给 IAM 用户?
- excel - 在列中搜索值以将指定数据复制到新选项卡(结构)
- python - 如果在 for 循环中 Python 超时
- python - 未调用 XGBoost fit() 自定义 eval_metric。为什么?
- google-chrome - Google Chrome 版本 83.0.4103.97 无法在 Windows 10 上访问/打开相机
- c# - 如何在可观察的反应式扩展中记住以前的有效负载?
- jpa - 无状态 EJB 如何回滚 JPA 事务