英文:
Accessing a nested value in a Go map
问题
我有一个随机的 JSON(我事先不知道模式),我将其编组为 map[string]interface{}
。
我还有一个表示要返回的字段值的字符串,类似于 "SomeRootKey.NestValue.AnotherNestValue"
。
我想要能够返回该值。有没有一种简单的方法可以访问该值,而不需要使用递归技巧?
英文:
I have a random JSON (I will not know the schema ahead of time) that I am marshaling into an map[string]interface{}
.
I also have a string representing the field value I would like to return, something like "SomeRootKey.NestValue.AnotherNestValue"
I want to be able to return that value. Is there an simple way to access that value without doing some recursive tricks?
答案1
得分: 1
不使用递归?是的,可以使用循环,但没有神奇的方法可以做到。
func getKey(m interface{}, key string) (string, bool) {
L:
for _, k := range strings.Split(key, ".") {
var v interface{}
switch m := m.(type) {
case map[string]interface{}:
v = m[k]
case []interface{}:
idx, err := strconv.Atoi(k)
if err != nil || idx > len(m) {
break L
}
v = m[idx]
default:
break L
}
switch v := v.(type) {
case map[string]interface{}:
m = v
case []interface{}:
m = v
case string:
return v, true
default:
break L
}
}
return "", false
}
使用类似以下的 JSON 数据:
{
"SomeRootKey": {
"NestValue": {"AnotherNestValue": "object value"},
"Array": [{"AnotherNestValue": "array value"}]
}
}
你可以这样使用:
fmt.Println(getKey(m, "SomeRootKey.NestValue.AnotherNestValue"))
fmt.Println(getKey(m, "SomeRootKey.Array.0.AnotherNestValue"))
[kbd] playground [/kbd]
英文:
Without recursion? yes, using a loop, but no there's no magical way to do that.
func getKey(m interface{}, key string) (string, bool) {
L:
for _, k := range strings.Split(key, ".") {
var v interface{}
switch m := m.(type) {
case map[string]interface{}:
v = m[k]
case []interface{}:
idx, err := strconv.Atoi(k)
if err != nil || idx > len(m) {
break L
}
v = m[idx]
default:
break L
}
switch v := v.(type) {
case map[string]interface{}:
m = v
case []interface{}:
m = v
case string:
return v, true
default:
break L
}
}
return "", false
}
Using json like:
{
"SomeRootKey": {
"NestValue": {"AnotherNestValue": "object value"},
"Array": [{"AnotherNestValue": "array value"}]
}
}
You can use:
fmt.Println(getKey(m, "SomeRootKey.NestValue.AnotherNestValue"))
fmt.Println(getKey(m, "SomeRootKey.Array.0.AnotherNestValue"))
<kbd>playground</kbd>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论