gjson

package
v1.4.7 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2019 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package gjson provides quite flexible and useful API for JSON/XML/YAML/TOML content handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(b []byte) (interface{}, error)

解码字符串为interface{}变量

func DecodeTo

func DecodeTo(b []byte, v interface{}) error

解析json字符串为go变量,注意第二个参数为指针(任意结构的变量)

func Encode

func Encode(v interface{}) ([]byte, error)

编码go变量为json字符串,并返回json字符串指针

Types

type Json

type Json struct {
	// contains filtered or unexported fields
}

json解析结果存放数组

func DecodeToJson

func DecodeToJson(b []byte) (*Json, error)

解析json字符串为gjson.Json对象,并返回操作对象指针

func Load

func Load(path string) (*Json, error)

支持多种配置文件类型转换为json格式内容并解析为gjson.Json对象

func LoadContent

func LoadContent(data []byte, dataType ...string) (*Json, error)

支持的配置文件格式:xml, json, yaml/yml, toml, 默认为自动识别,当无法检测成功时使用json解析。

func New

func New(value interface{}, unsafe ...bool) *Json

将变量转换为Json对象进行处理,该变量至少应当是一个map或者slice,否者转换没有意义

func NewUnsafe

func NewUnsafe(value ...interface{}) *Json

创建一个非并发安全的Json对象

func (*Json) Append

func (j *Json) Append(pattern string, value interface{}) error

指定pattern追加元素

func (*Json) Dump

func (j *Json) Dump() error

打印Json对象

func (*Json) Get

func (j *Json) Get(pattern ...string) interface{}

根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0"; 当pattern为空时,表示获取所有数据; 返回的结果类型的interface{},因此需要自己做类型转换; 如果找不到对应节点的数据,返回nil;

func (*Json) GetArray

func (j *Json) GetArray(pattern string) []interface{}

获得一个数组[]interface{},方便操作,不需要自己做类型转换 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil

func (*Json) GetBool

func (j *Json) GetBool(pattern string) bool

返回指定json中的bool(false:"", 0, false, off)

func (*Json) GetFloat32

func (j *Json) GetFloat32(pattern string) float32

func (*Json) GetFloat64

func (j *Json) GetFloat64(pattern string) float64

func (*Json) GetFloats

func (j *Json) GetFloats(pattern string) []float64

func (*Json) GetInt

func (j *Json) GetInt(pattern string) int

func (*Json) GetInt16

func (j *Json) GetInt16(pattern string) int16

func (*Json) GetInt32

func (j *Json) GetInt32(pattern string) int32

func (*Json) GetInt64

func (j *Json) GetInt64(pattern string) int64

func (*Json) GetInt8

func (j *Json) GetInt8(pattern string) int8

func (*Json) GetInterfaces

func (j *Json) GetInterfaces(pattern string) []interface{}

func (*Json) GetInts

func (j *Json) GetInts(pattern string) []int

func (*Json) GetJson

func (j *Json) GetJson(pattern string) *Json

将检索值转换为Json对象指针返回

func (*Json) GetMap

func (j *Json) GetMap(pattern string) map[string]interface{}

获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil

func (*Json) GetString

func (j *Json) GetString(pattern string) string

返回指定json中的string

func (*Json) GetStrings

func (j *Json) GetStrings(pattern string) []string

返回指定json中的strings(转换为[]string数组)

func (*Json) GetTime

func (j *Json) GetTime(pattern string, format ...string) time.Time

func (*Json) GetTimeDuration

func (j *Json) GetTimeDuration(pattern string) time.Duration

func (*Json) GetToStruct

func (j *Json) GetToStruct(pattern string, objPointer interface{}) error

将指定变量转换为struct对象(对象属性赋值)

func (*Json) GetToVar

func (j *Json) GetToVar(pattern string, v interface{}) error

将指定的json内容转换为指定结构返回,查找失败或者转换失败,目标对象转换为nil 注意第二个参数需要给的是**变量地址**

func (*Json) GetUint

func (j *Json) GetUint(pattern string) uint

func (*Json) GetUint16

func (j *Json) GetUint16(pattern string) uint16

func (*Json) GetUint32

func (j *Json) GetUint32(pattern string) uint32

func (*Json) GetUint64

func (j *Json) GetUint64(pattern string) uint64

func (*Json) GetUint8

func (j *Json) GetUint8(pattern string) uint8

func (*Json) Len

func (j *Json) Len(pattern string) int

计算指定pattern的元素长度(pattern对应数据类型为map[string]interface{}/[]interface{}时有效)

func (*Json) Remove

func (j *Json) Remove(pattern string) error

动态删除层级变量

func (*Json) Set

func (j *Json) Set(pattern string, value interface{}) error

动态设置层级变量

func (*Json) SetSplitChar

func (j *Json) SetSplitChar(char byte)

设置自定义的层级分隔符号

func (*Json) SetViolenceCheck

func (j *Json) SetViolenceCheck(check bool)

设置是否执行层级冲突检查,当键名中存在层级符号时需要开启该特性,默认为关闭。 开启比较耗性能,也不建议允许键名中存在分隔符,最好在应用端避免这种情况。

func (*Json) ToArray

func (j *Json) ToArray() []interface{}

转换为[]interface{}类型,如果转换失败,返回nil

func (*Json) ToJson

func (j *Json) ToJson() ([]byte, error)

func (*Json) ToJsonIndent

func (j *Json) ToJsonIndent() ([]byte, error)

func (*Json) ToMap

func (j *Json) ToMap() map[string]interface{}

转换为map[string]interface{}类型,如果转换失败,返回nil

func (*Json) ToStruct

func (j *Json) ToStruct(o interface{}) error

转换为指定的struct对象

func (*Json) ToToml

func (j *Json) ToToml() ([]byte, error)

func (*Json) ToXml

func (j *Json) ToXml(rootTag ...string) ([]byte, error)

func (*Json) ToXmlIndent

func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error)

func (*Json) ToYaml

func (j *Json) ToYaml() ([]byte, error)

Jump to

Keyboard shortcuts

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