如何在深度嵌套的 JSON 中将所有 child ID 与其 parent ID 进行比较?
How to compare all the child ID with their parent ID, In a deeply nested JSON?
这是我需要验证的 JSON 文档。
我必须检查 Children 中的所有 parent_id 是否正确。
如果所有 parent 和 child ID 都正确,我将 return 一个“有效”字符串。
{
"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
}
]
}
]
}
]
}
我用这段代码来迭代。但比较不成功。我从一个正在进行的堆栈溢出问题中得到了这段代码。我对从哪里开始有点困惑。
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))
这是我需要验证的 JSON 文档。 我必须检查 Children 中的所有 parent_id 是否正确。 如果所有 parent 和 child ID 都正确,我将 return 一个“有效”字符串。
{
"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
}
]
}
]
}
]
}
我用这段代码来迭代。但比较不成功。我从一个正在进行的堆栈溢出问题中得到了这段代码。我对从哪里开始有点困惑。
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))