如何通过 map[string]interface{} 递归迭代

我遇到了一个问题,即如何使用附加条件递归地遍历 map[string]interface{}。

1) 如果一个值是一个映射 - 递归调用该方法

2) 如果一个值是一个数组——调用数组的方法

3)如果一个值不是地图 - 处理它。

现在当方法尝试执行时doc.throughMap(mv)- 发生错误那么如何在reflect确认值是映射或数组后将某些值转换为所需的类型?

type MapType map[string]interface{}

type ArrayType []interface{}

func (doc *Document) throughMap(docMap MapType) MapType {

    for k, v := range docMap {

        vt := reflect.TypeOf(v)

        switch vt.Kind() {

        case reflect.Map:

            if mv, ok := v.(map[string]interface{}); ok {

                docMap[k] = doc.throughMap(mv)

            } else {

                panic("error.")

            }

        case reflect.Array, reflect.Slice:

            if mv, ok := v.([]interface{}); ok {

                docMap[k] = doc.throughArray(mv)

            } else {

                panic("error.")

            }

        default:

            docMap[k] = doc.processType(v)

        }

    }

    return docMap

}

堆栈跟踪:


panic: error. [recovered]

    panic: error.


goroutine 1 [running]:

encoding/json.(*encodeState).marshal.func1(0xc000074cd0)

    /usr/local/go/src/encoding/json/encode.go:301 +0x9a

panic(0x4bd700, 0x4f9b70)

    /usr/local/go/src/runtime/panic.go:513 +0x1b9

project-name/package/name.(*Document).throughMap(0xc00000c028, 0xc000060180, 0xc00007e000)

    /home/path/to/project/document.go:231 +0x3f4

project-name/package/name.(*Document).convertDocument(0xc00000c028)

    /home/path/to/project/document.go:217 +0x33

project-name/pachage/name.(*Document).MarshalJSON(0xc00000c028, 0x4db740, 0xc00000c028, 0x7f3f0f7540c0, 0xc00000c028, 0xc00001c101)



慕斯709654
浏览 130回答 2
2回答

慕妹3242003

使用以下代码递归映射、数组和任何类型的切片:func walk(v reflect.Value) {&nbsp; &nbsp; fmt.Printf("Visiting %v\n", v)&nbsp; &nbsp; // Indirect through pointers and interfaces&nbsp; &nbsp; for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {&nbsp; &nbsp; &nbsp; &nbsp; v = v.Elem()&nbsp; &nbsp; }&nbsp; &nbsp; switch v.Kind() {&nbsp; &nbsp; case reflect.Array, reflect.Slice:&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < v.Len(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; walk(v.Index(i))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; case reflect.Map:&nbsp; &nbsp; &nbsp; &nbsp; for _, k := range v.MapKeys() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; walk(v.MapIndex(k))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; // handle other types&nbsp; &nbsp; }}

慕慕森

以下是为我工作func main() {&nbsp; &nbsp; x := MapType{&nbsp; &nbsp; &nbsp; &nbsp; "a": MapType{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "x": MapType{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "p": ArrayType{"l", "o", "l"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; } ,&nbsp; &nbsp; }&nbsp; &nbsp; d := &Document{}&nbsp; &nbsp; fmt.Println(d.throughMap(x))}type Document struct {}type MapType map[string]interface{}type ArrayType []interface{}func (doc *Document) throughMap(docMap MapType) MapType {&nbsp; &nbsp; for k, v := range docMap {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(k, v)&nbsp; &nbsp; &nbsp; &nbsp; vt := reflect.TypeOf(v)&nbsp; &nbsp; &nbsp; &nbsp; switch vt.Kind() {&nbsp; &nbsp; &nbsp; &nbsp; case reflect.Map:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if mv, ok := v.(MapType); ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docMap[k] = doc.throughMap(mv)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic("error.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; case reflect.Array, reflect.Slice:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if mv, ok := v.(ArrayType); ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docMap[k] = doc.throughArray(mv)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic("error.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docMap[k] = doc.processType(v)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return docMap}func (doc *Document) throughArray(arrayType ArrayType) ArrayType&nbsp; {&nbsp; &nbsp; return arrayType}func (doc *Document) processType(x interface{}) interface{} {&nbsp; &nbsp; return x}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go