利用类型解组来递归解析 JSON 数据
来源:stackoverflow
时间:2024-03-02 20:36:28 327浏览 收藏
有志者,事竟成!如果你在学习Golang,那么本文《利用类型解组来递归解析 JSON 数据》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
我有一个具有以下形式的 json:
{
"Filter": {
"Filters": [
{
"Filter": {
"someKey": "someValue1",
},
"FilterType": "trait"
},
{
"Filter": {
"someKey": "someValue2",
},
"FilterType": "trait"
}
]
},
"FilterType": "and" // this can be an or too
}
每个过滤器都有一个类型,该类型映射到我在代码中获得的特定结构。正如您所看到的,这具有递归性质:过滤器可以是带有多个子过滤器的 and (也可以是 ands 或 ors 等)。
因此,根据类型,我需要递归地将其映射到正确的结构。有什么建议吗?我研究过使用映射结构,但即使如此,代码也变得非常糟糕。
请注意,此示例仅使用 and 和 trait 过滤器,但假设还有更多类似 or 的过滤器
正确答案
这种类型的多态 json 很常见,并且通常不容易处理。我在这里解释的是一种使用单独的结构来实现此目的的方法,该结构仅用于封送,并依赖于后处理来构造过滤器。
您可以定义一个包含过滤器所有可能字段的 filtermarshal 结构:
type filtermarshal struct {
filtertype string `json:"filtertype"`
filters []*filtermarshal `json:"filters"`
filter *filtermarshal `json:"filter"`
somekey string `json:"somekey"`
...
}
此实现假设不存在名称冲突。例如,如果过滤器类型将 filter 字段作为一种类型,而另一种类型将 filter 字段作为不兼容类型,则这将不起作用,您必须使用其他方法。
当您解组 filtermarshal 中的 json 时,您将获得与输入 json 匹配的对象树。然后,您可以构建实际的过滤器结构:
func unmarshalfilter(input *filtermarshal) filter {
switch input.filtertype {
case "and":
// process input.filters recursively
return andfilter{filters: processfilters(input.filters))
case "trait":
return traitfilter{somekey: input.somekey}
}
}在没有澄清功能规范的情况下,您需要使用此结构来表示与您在问题中发布的 json 相近的内容。
type filtercontainer struct {
filter map[string]interface{} `json:"filter,omitempty"`
filters []filtercontainer `json:"filters,omitempty"`
filtertype string `json:"filtertype"`
}
你的过滤器将像这样在 go 中初始化
f := filtercontainer{
filters: []filtercontainer{
{
filter: map[string]interface{}{"somekey": "somevalue1"},
filtertype: "trait",
},
{
filter: map[string]interface{}{"somekey": "somevalue2"},
filtertype: "trait",
},
},
filtertype: "and",
}
当过滤器被编组为 json 并漂亮地打印时,它们看起来像这样
{
"filters": [
{
"filter": {
"somekey": "somevalue1"
},
"filtertype": "trait"
},
{
"filter": {
"somekey": "somevalue2"
},
"filtertype": "trait"
}
],
"filtertype": "and"
}
上面的 json 与您问题中的 json 不完全匹配;它缺少顶级 filter 字段。我认为我在这个答案中提出的结构将简化实现以及心智模型。
在 https://go.dev/play/p/6cLPT28DG2O 的 go 演示中尝试一下
如果您确实非常需要匹配问题中的 json
这是您需要的结构
type filtercontainer struct {
filter interface{} `json:"filter,omitempty"` // either a map[string]interface{} or a filtercontainer
filters []filtercontainer `json:"filters,omitempty"`
filtertype string `json:"filtertype,omitempty"`
}
它是这样填充的
f := FilterContainer{
Filter: FilterContainer{
Filters: []FilterContainer{
{
Filter: map[string]interface{}{"someKey": "someValue1"},
FilterType: "trait",
},
{
Filter: map[string]interface{}{"someKey": "someValue2"},
FilterType: "trait",
},
},
},
FilterType: "and",
}
在 https://go.dev/play/p/uB_LmJc6NGH 的 go 演示中尝试一下
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习