获取特定键的值

Getting a value of a specific key

我正在从数据库中获取这样的字符串。

[{"Key":"a","Value":"4521"},{"Key":"b","Value":"7"}]

我想获取键“b”的值。在 Go 中执行此操作的最佳方法是什么?

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

func main() {
    str := `[{"Key":"a","Value":"4521"},{"Key":"b","Value":"7"}]`

    // declaring out struct we will use for unmarshaling and iteration check.
    out := []struct {
        Key, Value string
    }{}

    if err := json.Unmarshal([]byte(str), &out); err != nil {
        log.Fatal(err)
    } else {
        // searching for value.
        for i := range out {
            if out[i].Key == "b" {
                fmt.Println("Found", out[i].Value)
                return
            }
        }
    }
}

这是一种简单的方法,但不是最佳方法。最佳伤口是手动逐字节解析字符串。