文档: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) bool
func ValidBytes(json []byte) bool
func Get(json, path string) Result
func GetBytes(json []byte, path string) Result
func GetMany(json string, path ...string) []Result
func GetManyBytes(json []byte, path ...string) []Result
func Parse(json string) Result
func ParseBytes(json []byte) Result
type Result
type Result struct {
// Type is the json type
Type Type
// Raw is the raw json
Raw string
// Str is the json string
Str string
// Num is the json number
Num float64
// Index of raw value in original json, zero means index unknown
Index int
// Indexes of all the elements that match on a path containing the '#'
// query character.
Indexes []int
}
// Array返回一个值的数组。如果结果表示空值或不存在,则返回一个空数组。
// 如果结果不是一个JSON数组,返回值将是一个包含一个结果的数组。
func (t Result) Array() []Result
func (t Result) Bool() bool
func (t Result) Exists() bool
func (t Result) Float() float64
func (t Result) String() string
func (t Result) Time() time.Time
func (t Result) Uint() uint64=
func (t Result) Int() int64
func (t Result) IsArray() bool
func (t Result) IsBool() bool
func (t Result) IsObject() bool
func (t Result) Map() map[string]Result
func (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 int
const (
// Null is a null json value
Null Type = iota
// False is a json false boolean
False
// Number is json number
Number
// String is a json string
String
// True is a json true boolean
True
// JSON is a raw block of JSON
JSON
)
func (t Type) String() string
Get a value
package main
import "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.# 3
friends.#.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