如何从Go中的json字符串获取键值

时间:2022-10-26 11:15:02

I would like to try get the key values from JSON in Go, however I'm unsure how to.

我想尝试从Go中获取JSON的关键值,但是我不确定如何。

I've been able to use simplejson to read json values, however I've not been able to find out how to get the key values.

我已经能够使用simplejson来读取json值,但是我无法找到如何获取键值。

Would anyone be able to point me in the right direction and/or help me?

有人能指出我正确的方向和/或帮助我吗?

Thank you!

3 个解决方案

#1


6  

You can get the top-level keys of a JSON structure by doing:

您可以通过执行以下操作获取JSON结构的*键:

package main

import (
    "encoding/json"
    "fmt"
)

// your JSON structure as a byte slice
var j = []byte(`{"foo":1,"bar":2,"baz":[3,4]}`)

func main() {

    // a map container to decode the JSON structure into
    c := make(map[string]interface{})

    // unmarschal JSON
    e := json.Unmarshal(j, &c)

    // panic on error
    if e != nil {
        panic(e)
    }

    // a string slice to hold the keys
    k := make([]string, len(c))

    // iteration counter
    i := 0

    // copy c's keys into k
    for s, _ := range c {
        k[i] = s
        i++
    }

    // output result to STDOUT
    fmt.Printf("%#v\n", k)

}

Note that the order of the keys must not correspond to the their order in the JSON structure. Their order in the final slice will even vary between different runs of the exact same code. This is because of how map iteration works.

请注意,键的顺序不得与JSON结构中的顺序相对应。它们在最终切片中的顺序甚至会在完全相同的代码的不同运行之间变化。这是因为地图迭代的工作原理。

#2


5  

There's also this incredible library: https://github.com/jmoiron/jsonq

还有这个令人难以置信的库:https://github.com/jmoiron/jsonq

It lets you fetch nested values from JSON by calling:

它允许您通过调用以下方法从JSON获取嵌套值:

// data["subobj"]["subarray"]["array"][0] -> "hello"
jq.String("subobj", "subsubobj", "array", "0")

#3


0  

I used the following to grab nested keys from JSON:

我使用以下方法从JSON中获取嵌套键:

import (
    "bytes"
    "encoding/json"
    "errors"
    "io"
    "sort"
)

func keys(b []byte) ([]string, error) {
    dec := json.NewDecoder(bytes.NewBuffer(b))
    // store unique keys
    kmap := make(map[string]struct{})
    // is the next Token a key?
    var key bool
    // keep track of both object and array parents with a slice of bools:
    //   - an object parent is true, an array parent is false
    parents := make([]bool, 0, 10)
    for {
        t, err := dec.Token()
        if err == io.EOF {
            break
        }
        if err != nil {
            return nil, err
        }
        del, ok := t.(json.Delim)
        if ok {
            if del == '{' {
                // push an object parent
                parents = append(parents, true)
            }
            if del == '[' {
                // push an array parent
                parents = append(parents, false)
            }
            if del == '}' || del == ']' {
                if len(parents) == 0 {
                    return nil, errors.New("bad json: unexpected } or ] delim")
                }
                // pop the last parent
                parents = parents[:len(parents)-1]
            }
            if len(parents) > 0 && parents[len(parents)-1] {
                // if we are within an object, the next token must be a key
                key = true
            } else {
                // otherwise we are in an array, and the next token is an array entry
                key = false
            }
            continue
        }
        if key {
            str, ok := t.(string)
            if !ok {
                return nil, errors.New("bad json: keys must be strings")
            }
            kmap[str] = struct{}{}
            // if this is a key, then the next token is the value
            key = false
        } else if len(parents) > 0 && parents[len(parents)-1] {
            // if this is a value, and we are within an object, then the next token is a new key
            key = true
        }
    }
    // now turn our map of keys into a sorted slice
    ret := make([]string, len(kmap))
    var i int
    for k := range kmap {
        ret[i] = k
        i++
    }
    sort.Strings(ret)
    return ret, nil
}

#1


6  

You can get the top-level keys of a JSON structure by doing:

您可以通过执行以下操作获取JSON结构的*键:

package main

import (
    "encoding/json"
    "fmt"
)

// your JSON structure as a byte slice
var j = []byte(`{"foo":1,"bar":2,"baz":[3,4]}`)

func main() {

    // a map container to decode the JSON structure into
    c := make(map[string]interface{})

    // unmarschal JSON
    e := json.Unmarshal(j, &c)

    // panic on error
    if e != nil {
        panic(e)
    }

    // a string slice to hold the keys
    k := make([]string, len(c))

    // iteration counter
    i := 0

    // copy c's keys into k
    for s, _ := range c {
        k[i] = s
        i++
    }

    // output result to STDOUT
    fmt.Printf("%#v\n", k)

}

Note that the order of the keys must not correspond to the their order in the JSON structure. Their order in the final slice will even vary between different runs of the exact same code. This is because of how map iteration works.

请注意,键的顺序不得与JSON结构中的顺序相对应。它们在最终切片中的顺序甚至会在完全相同的代码的不同运行之间变化。这是因为地图迭代的工作原理。

#2


5  

There's also this incredible library: https://github.com/jmoiron/jsonq

还有这个令人难以置信的库:https://github.com/jmoiron/jsonq

It lets you fetch nested values from JSON by calling:

它允许您通过调用以下方法从JSON获取嵌套值:

// data["subobj"]["subarray"]["array"][0] -> "hello"
jq.String("subobj", "subsubobj", "array", "0")

#3


0  

I used the following to grab nested keys from JSON:

我使用以下方法从JSON中获取嵌套键:

import (
    "bytes"
    "encoding/json"
    "errors"
    "io"
    "sort"
)

func keys(b []byte) ([]string, error) {
    dec := json.NewDecoder(bytes.NewBuffer(b))
    // store unique keys
    kmap := make(map[string]struct{})
    // is the next Token a key?
    var key bool
    // keep track of both object and array parents with a slice of bools:
    //   - an object parent is true, an array parent is false
    parents := make([]bool, 0, 10)
    for {
        t, err := dec.Token()
        if err == io.EOF {
            break
        }
        if err != nil {
            return nil, err
        }
        del, ok := t.(json.Delim)
        if ok {
            if del == '{' {
                // push an object parent
                parents = append(parents, true)
            }
            if del == '[' {
                // push an array parent
                parents = append(parents, false)
            }
            if del == '}' || del == ']' {
                if len(parents) == 0 {
                    return nil, errors.New("bad json: unexpected } or ] delim")
                }
                // pop the last parent
                parents = parents[:len(parents)-1]
            }
            if len(parents) > 0 && parents[len(parents)-1] {
                // if we are within an object, the next token must be a key
                key = true
            } else {
                // otherwise we are in an array, and the next token is an array entry
                key = false
            }
            continue
        }
        if key {
            str, ok := t.(string)
            if !ok {
                return nil, errors.New("bad json: keys must be strings")
            }
            kmap[str] = struct{}{}
            // if this is a key, then the next token is the value
            key = false
        } else if len(parents) > 0 && parents[len(parents)-1] {
            // if this is a value, and we are within an object, then the next token is a new key
            key = true
        }
    }
    // now turn our map of keys into a sorted slice
    ret := make([]string, len(kmap))
    var i int
    for k := range kmap {
        ret[i] = k
        i++
    }
    sort.Strings(ret)
    return ret, nil
}