比较深度嵌套的 JSON 中的子 ID 和父 ID 的关系
来源:stackoverflow
时间:2024-02-23 21:27:24 470浏览 收藏
从现在开始,努力学习吧!本文《比较深度嵌套的 JSON 中的子 ID 和父 ID 的关系》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!
问题内容
这是我需要验证的 json 文档。 我必须检查 children 中的所有parent_id 是否正确。 如果所有父项和子项 id 均正确,我将返回一个“有效”字符串。
{
"id": 10,
"children": [
{
"id": 25,
"parent_id": 10,
"children": [
{
"id": 131,
"parent_id": 25,
"children": null
},
{
"id": 33,
"parent_id": 25,
"children": [
{
"id": 138,
"parent_id": 33,
"children": null
},
{
"id": 139,
"parent_id": 33,
"children": null
},
{
"id": 140,
"parent_id": 33,
"children": null
},
{
"id": 160,
"parent_id": 33,
"children": null
},
{
"id": 34,
"parent_id": 33,
"children": [
{
"id": 26,
"parent_id": 34,
"children": null
},
{
"id": 64,
"parent_id": 34,
"children": null
},
{
"id": 65,
"parent_id": 34,
"children": null
}
]
},
{
"id": 35,
"parent_id": 33,
"children": null
},
{
"id": 36,
"parent_id": 33,
"children": null
},
{
"id": 38,
"parent_id": 33,
"children": null
},
{
"id": 39,
"parent_id": 33,
"children": null
},
{
"id": 40,
"parent_id": 33,
"children": null
},
{
"id": 41,
"parent_id": 33,
"children": null
},
{
"id": 42,
"parent_id": 33,
"children": null
},
{
"id": 148,
"parent_id": 33,
"children": null
}
]
},
{
"id": 66,
"parent_id": 25,
"main_id": 66,
"children": [
{
"id": 144,
"parent_id": 66,
"children": null
},
{
"id": 146,
"parent_id": 66,
"children": null
},
{
"id": 147,
"parent_id": 66,
"children": null
},
{
"id": 67,
"parent_id": 66,
"children": null
}
]
}
]
}
]
}
我使用此代码进行迭代。但比较没有成功。我从 go 上的堆栈溢出问题之一得到了这段代码。我有点困惑从哪里开始。
func parseMap(aMap map[string]interface{}) {
for key, val := range aMap {
switch concreteVal := val.(type) {
case map[string]interface{}:
// if key == "id" {
fmt.Println(key)
// }
parseMap(val.(map[string]interface{}))
case []interface{}:
// if key == "id" {
fmt.Println(key)
// }
parseArray(val.([]interface{}))
default:
fmt.Println(key, ":", concreteVal)
// if key == "parent_id" || key == "main_id" {
// }
}
}
}
func parseArray(anArray []interface{}) {
for i, val := range anArray {
switch concreteVal := val.(type) {
case map[string]interface{}:
fmt.Println("Index:", i)
parseMap(val.(map[string]interface{}))
case []interface{}:
fmt.Println("Index:", i)
parseArray(val.([]interface{}))
default:
fmt.Println("Index", i, ":", concreteVal)
}
}
}
func main(w http.ResponseWriter, r *http.Request) {
// data, err := ioutil.ReadAll(r.Body)
m := map[string]interface{}{}
err := json.Unmarshal([]byte(input), &m)
if err != nil {
panic(err)
}
parseMap(m)
}
我从 api 获取值,但现在我正在硬编码 json 值
解决方案
定义与数据结构匹配的类型:
type node struct {
id int `json:"id"`
parentid int `json:"parent_id"`
mainid int `json:"main_id"`
children []*node `json:"children"`
}
编写一个递归函数来检查数据:
func check(n *node) bool {
for _, c := range n.children {
if c.parentid != n.id {
return false
}
if !check(c) {
return false
}
}
return true
}
将 json 解组为该类型的值。检查结果。
var n node
err := json.Unmarshal(data, &n)
if err != nil {
log.Fatal(err)
}
fmt.Println(check(&n))
Run it on the GoLang PlayGround。
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《比较深度嵌套的 JSON 中的子 ID 和父 ID 的关系》文章吧,也可关注golang学习网公众号了解相关技术文章。
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
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次学习