卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35664本站已运行398

如何进行嵌套迭代

如何进行嵌套迭代

php小编柚子为您介绍如何进行嵌套迭代。嵌套迭代是一种在循环中使用另一个循环的技术,它可以帮助我们处理复杂的数据结构或多维数组。在进行嵌套迭代时,我们需要注意循环的顺序和条件,以确保正确地访问和处理每个元素。本文将为您详细解释嵌套迭代的原理和使用方法,并提供一些实用的示例供参考。无论您是初学者还是有一定经验的开发者,本文都能帮助您更好地理解和运用嵌套迭代的技巧。让我们一起来探索吧!

问题内容

我正在尝试开发另一个软件的扩展,该软件将请求发送到用 go 编写的应用程序。在 go 程序(我现在将其称为“程序”)中,一个目的是将 json 文件转换为可迭代的格式。以下是我正在使用的 json 格式示例:

{
  "name": "game-name",
  "tree": {
    "$classname": "datamodel",

    "replicatedstorage": {
      "$path": "src/replicatedstorage"
    },

    "serverscriptservice": {
      "$path": "src/serverscriptservice"
    },

    "replicatedfirst": {
      "$path": "src/replicatedfirst"
    },

    "serverstorage": {
      "$path": "src/serverstorage"
    }
  }
}

这个想法是:

  • 迭代可以获取“名称”
  • 迭代可以获取“$classname”
  • 对于所有以“$path”作为索引的实例,都会在父 src 文件夹下创建一个文件夹,其中包含父地图的索引。例如,replicatedstorage 是路径为 src/replicatedstorage 的文件夹的名称

下面是一个用于执行此操作的处理函数:

func process(in interface{}) {
v := reflect.ValueOf(in)

    if v.Kind() == reflect.Map {
        for _, key := range v.MapKeys() {
            strct := v.MapIndex(key)
    
            index := key.Interface()
            value := reflect.ValueOf(strct.Interface())
    
            if index == "tree" {
                for _, treeKey := range value.MapKeys() {
                    treeIndex := treeKey.Interface()
    
                    fmt.Println("KEY")
                    fmt.Println(treeIndex)
    
                    if treeIndex != "$className" {
                        fmt.Println("bug")
                        fmt.Println(treeKey)
    
                        a := key.MapIndex(value) // panic serving ...: reflect: call of reflect.Value.MapIndex on string Value
                        b := reflect.ValueOf(a.Interface())
    
                        for _, key2 := range b.MapKeys() {
                            index2 := key2.Interface()
                            value2 := reflect.ValueOf(key2.Interface())
    
                            fmt.Println(index2)
                            fmt.Println(value2)
                        }
                    }
                }
            }
        }
    }

}

评论指出了错误的位置和内容。我还想做的一件事是不必堆叠 for 循环,因为那是非常糟糕的代码。

解决方法

通常的方法是解组为与数据结构匹配的 go 类型。这里的问题是,树不能轻易地表示为 go 类型(它具有字符串类型的字段 $classname,但在其他方面类似于具有包含 $path 字段的对象值的映射)。

让我们像您已经完成的那样继续解组到 interface{}

使用类型断言而不是反射包。使用映射索引来查找值,而不是循环遍历键并查找匹配项。

func process(in interface{}) error {
    top, ok := in.(map[string]interface{})
    if !ok {
        return errors.New("expected object at top level")
    }
    tree, ok := top["tree"].(map[string]interface{})
    if !ok {
        return errors.New(".tree not found")
    }
    name, ok := top["name"]
    if !ok {
        return errors.New(".name not found")
    }
    className, ok := tree["$className"].(string)
    if !ok {
        return errors.New(".tree.$className not found")
    }
    for k, v := range tree {
        thing, ok := v.(map[string]interface{})
        if !ok {
            continue
        }
        path, ok := thing["$path"].(string)
        if !ok {
            continue
        }
        fmt.Println(name, className, k, path)
    }
    return nil
}

https://www.php.cn/link/8642785813491d703d517ddd00944054

卓越飞翔博客
上一篇: 了解路由时 Go 代理失败
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏