在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jmoiron/jsonq开源软件地址:https://github.com/jmoiron/jsonq开源编程语言:Go 97.9%开源软件介绍:jsonqSimplify your golang json usage by extracting fields or items from arrays and objects with a simple, hierarchical query. API Documentation on godoc.org. This package is meant to make working with complex feeds a bit more easy. If you have simple feeds you want to model with struct types, check out jflect, which will create struct definitions given a json document. installing
usageGiven some json data like: {
"foo": 1,
"bar": 2,
"test": "Hello, world!",
"baz": 123.1,
"array": [
{"foo": 1},
{"bar": 2},
{"baz": 3}
],
"subobj": {
"foo": 1,
"subarray": [1,2,3],
"subsubobj": {
"bar": 2,
"baz": 3,
"array": ["hello", "world"]
}
},
"bool": true
} Decode it into a import (
"strings"
"encoding/json"
"github.com/jmoiron/jsonq"
)
data := map[string]interface{}{}
dec := json.NewDecoder(strings.NewReader(jsonstring))
dec.Decode(&data)
jq := jsonq.NewQuery(data) From here, you can query along different keys and indexes: // data["foo"] -> 1
jq.Int("foo")
// data["subobj"]["subarray"][1] -> 2
jq.Int("subobj", "subarray", "1")
// data["subobj"]["subarray"]["array"][0] -> "hello"
jq.String("subobj", "subsubobj", "array", "0")
// data["subobj"] -> map[string]interface{}{"subobj": ...}
obj, err := jq.Object("subobj") Missing keys, out of bounds indexes, and type failures will return errors.
For simplicity, integer keys (ie, {"0": "zero"}) are inaccessible
by The Suggestions/comments please tweet @jmoiron |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论