query

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DisableModifiers = false

DisableModifiers will disable the modifier syntax

Functions

func AddModifier

func AddModifier(name string, fn func(json, arg string) string)

AddModifier 将一个自定义修饰符命令绑定到GJSON语法。 这个操作不是线程安全的,应该在使用所有其他查询函数之前执行。

func ForEachLine

func ForEachLine(json string, iterator func(line Result) bool)

ForEachLine 通过JSON lines指定的JSON行进行迭代

func ModifierExists

func ModifierExists(name string, fn func(json, arg string) string) bool

ModifierExists 当指定的修饰符存在时返回true。

func Valid

func Valid(json string) bool

Valid 如果输入的json是有效的,返回true。

func ValidBytes

func ValidBytes(json []byte) bool

ValidBytes 如果输入的json是有效的,返回true。

Types

type Query

type Query struct {
	// Get 根据路径从json字符串中查询数据
	Get func(json, path string) Result
}

Query 查询核心对象

func NewQuery

func NewQuery() *Query

type Result

type Result struct {
	Type    Type    // json类型
	Raw     string  // 格式化的json
	Str     string  // 字符串的json
	Num     float64 // json数字
	Index   int     // 对于原始json中的原始值,0表示索引未知
	Indexes []int   // 在包含“#”查询字符的路径上匹配的所有元素。
}

Result 表示从Get()返回的json值。

func Get

func Get(json, path string) Result

Get 在json中搜索指定路径。 路径使用.分割,比如:"name.last" 或 "age" 当找到值时,它会立即返回。

func GetBytes

func GetBytes(json []byte, path string) Result

GetBytes 在json中搜索指定路径。 如果使用字节,此方法优于Get(string(data), path)

func GetMany

func GetMany(json string, path ...string) []Result

GetMany 在json中搜索多个路径。 返回值是一个Result数组,其中项的数量将等于输入路径的数量。

func GetManyBytes

func GetManyBytes(json []byte, path ...string) []Result

GetManyBytes 在json中搜索多个路径 返回值是一个Result数组,其中项的数量将等于输入路径的数量。

func Parse

func Parse(json string) Result

Parse 解析json并返回一个结果。 这个函数期望json是格式良好的,并且不进行验证。 无效的json将不会异常,但它可能返回意想不到的结果。 如果您使用的JSON来自一个不可预测的来源,那么您可能希望首先使用Valid函数。

func ParseBytes

func ParseBytes(json []byte) Result

ParseBytes 解析json并返回一个结果。如果使用字节,此方法优于Parse(string(data))

func (Result) Array

func (t Result) Array() []Result

Array 返回一个array数组表示形式 如果结果表示空值或不存在,则返回一个空数组。 如果结果不是一个JSON数组,返回值将是一个包含一个结果的数组。

func (Result) Bool

func (t Result) Bool() bool

Bool 返回一个布尔表示。

func (Result) Exists

func (t Result) Exists() bool

Exists 如果值存在,返回true。

 if query.Get(json, "name.last").Exists(){
		println("value exists")
 }

func (Result) Float

func (t Result) Float() float64

Float 返回一个float64的表示形式

func (Result) ForEach

func (t Result) ForEach(iterator func(key, value Result) bool)

ForEach 遍历每个值 如果结果表示不存在的值,则不会迭代任何值。 如果结果是Object,迭代器将传递每个项的键和值。如果结果是Array,迭代器将只传递每一项的值。 如果结果不是JSON数组或对象,迭代器将返回一个等于结果的值。

func (Result) Get

func (t Result) Get(path string) Result

Get 查询指定路径的结果。结果应该是一个JSON数组或对象。

func (Result) Int

func (t Result) Int() int64

Int 返回一个int表示

func (Result) IsArray

func (t Result) IsArray() bool

IsArray 判断结果是不是一个JSON数组

func (Result) IsObject

func (t Result) IsObject() bool

IsObject 判断结果是不是JSON对象

func (Result) Less

func (t Result) Less(token Result, caseSensitive bool) bool

Less 如果一个令牌小于另一个令牌,则返回true。 当令牌为string时,使用caseSensitive参数。比较两种不同类型时的顺序为:

Null < False < Number < String < True < JSON

func (Result) Map

func (t Result) Map() map[string]Result

Map 返回值的映射。结果应该是一个JSON对象。如果结果不是一个JSON对象,返回值将是一个空映射。

func (Result) Path

func (t Result) Path(json string) string

Path returns the original GJSON path for Result. The json param must be the original JSON used when calling Get.

func (Result) Paths

func (t Result) Paths(json string) []string

func (Result) String

func (t Result) String() string

String 返回值的字符串表示形式。

func (Result) Time

func (t Result) Time() time.Time

Time 返回一个 time.Time 的表示形式

func (Result) Uint

func (t Result) Uint() uint64

Uint 返回无符号整数表示形式。

func (Result) Value

func (t Result) Value() interface{}

Value 返回以下类型之一: bool, float64, Number, string, nil, map[string]interface{}, []interface{}

type Type

type Type int

Type int的别名

const (
	Null   Type = iota // json中的null类型
	False              // json中的false类型
	Number             // json中的number类型
	String             // json中的string类型
	True               // json中的true类型
	JSON               // json中的嵌套json类型
)

func (Type) String

func (t Type) String() string

String 返回该类型的字符串表示形式。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL