文档:https://pkg.go.dev/github.com/tidwall/gjson
Func
func AddModifier(name string, fn func(json, arg string) string)func ForEachLine(json string, iterator func(line Result) bool)func Valid(json string) boolfunc ValidBytes(json []byte) boolfunc Get(json, path string) Resultfunc GetBytes(json []byte, path string) Resultfunc GetMany(json string, path ...string) []Resultfunc GetManyBytes(json []byte, path ...string) []Resultfunc Parse(json string) Resultfunc ParseBytes(json []byte) Result
type Result
type Result struct {// Type is the json typeType Type// Raw is the raw jsonRaw string// Str is the json stringStr string// Num is the json numberNum float64// Index of raw value in original json, zero means index unknownIndex int// Indexes of all the elements that match on a path containing the '#'// query character.Indexes []int}// Array返回一个值的数组。如果结果表示空值或不存在,则返回一个空数组。// 如果结果不是一个JSON数组,返回值将是一个包含一个结果的数组。func (t Result) Array() []Resultfunc (t Result) Bool() boolfunc (t Result) Exists() boolfunc (t Result) Float() float64func (t Result) String() stringfunc (t Result) Time() time.Timefunc (t Result) Uint() uint64=func (t Result) Int() int64func (t Result) IsArray() boolfunc (t Result) IsBool() boolfunc (t Result) IsObject() boolfunc (t Result) Map() map[string]Resultfunc (t Result) Path(json string) string // Path返回Result的原始GJSON路径func (t Result) Value() interface{}// bool, for JSON booleans// float64, for JSON numbers// Number, for JSON numbers// string, for JSON string literals// nil, for JSON null// map[string]interface{}, for JSON objects// []interface{}, for JSON arrays
type Type
type Type intconst (// Null is a null json valueNull Type = iota// False is a json false booleanFalse// Number is json numberNumber// String is a json stringString// True is a json true booleanTrue// JSON is a raw block of JSONJSON)func (t Type) String() string
Get a value
package mainimport "github.com/tidwall/gjson"const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`func main() {value := gjson.Get(json, "name.last")println(value.String()) // Prichard}// 下面三种写法,是相同的结果gjson.Parse(json).Get("name").Get("last")gjson.Get(json, "name").Get("last")gjson.Get(json, "name.last")
Path Syntax
*: 通配符
?: 占位符
\: 转义符
{"name": {"first": "Tom", "last": "Anderson"},"age":37,"children": ["Sara","Alex","Jack"],"fav.movie": "Deer Hunter","friends": [{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}]}"name.last" >> "Anderson""age" >> 37"children" >> ["Sara","Alex","Jack"]"children.#" >> 3"children.1" >> "Alex""child*.2" >> "Jack""c?ildren.0" >> "Sara""fav\.movie" >> "Deer Hunter""friends.#.first" >> ["Dale","Roger","Jane"]"friends.1.last" >> "Craig"
Arrays
friends.# 3friends.#.age [44,68,47]
Queries
(…):匹配第一个数组
#(…)#:匹配所有数组
支持 == != < <=``> >= % (like) !% (not like)
friends.#(last=="Murphy").first "Dale"friends.#(last=="Murphy")#.first ["Dale","Jane"]friends.#(age>45)#.last ["Craig","Murphy"]friends.#(first%"D*").last "Murphy"friends.#(first!%"D*").last "Craig"
使用非对象,查询元素不包含
children.#(!%"*a*") "Alex"children.#(%"*a*")# ["Sara","Jack"]
Modifiers
编辑器,例如下面反转数组
children.@reverse ["Jack","Alex","Sara"]children.@reverse.0 "Jack"
- @reverse: Reverse an array or the members of an object.
- @ugly: Remove all whitespace from JSON.
- @pretty: Make the JSON more human readable.
- @this: Returns the current element. It can be used to retrieve the root element.
- @valid: Ensure the json document is valid.
- @flatten: Flattens an array.
- @join: Joins multiple objects into a single object.
- @keys: Returns an array of keys for an object.
- @values: Returns an array of values for an object.
- @tostr: Converts json to a string. Wraps a json string.
- @fromstr: Converts a string from json. Unwraps a json string.
- @group: Groups arrays of objects
