EasyJSON

package module
v0.0.0-...-0c5f906 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2020 License: MIT Imports: 9 Imported by: 0

README

EasyJSONGo

一个十分容易使用的Go语言JSON库(解析JSON、生成JSON)

EasyJSON的Java版本

开始使用

引用库文件

使用go get下载

go get github.com/373518155/EasyJSONGo

然后在代码中引入库

import "github.com/373518155/EasyJSONGo"

使用示例,一行代码用过一百句废话
package main

import (
	"github.com/373518155/EasyJSONGo"
	"fmt"
)

func main()  {
	/*************************************
	解析JSON字符串
	 *************************************/
	jsonStr := `{
	"name": "Go in Action",
	"price": 138.27,
	"authors": ["Raoul-Gabriel Urma", "Mario Fusco", "Alan Mycroft"],
	"pages": 550,
	"available": true,
	"publisher": null,
	"chapters": [{
			"title": "Introduction",
			"pages": 22
		},
		{
			"title": "Basic Go",
			"pages": 33
		},
		{
			"title": "Advanced Go",
			"pages": 44
		}
	]
}`

	// 解析图书信息的JSON字符串,得到easyJSON对象
	easyJSON, _ := EasyJSON.Parse(jsonStr)
	// 获取书名
	name, _ := easyJSON.GetString("name")
	fmt.Println(name)  // 输出: Go in Action

	// 获取第3个作者
	author3, _ := easyJSON.GetString("authors[2]")
	fmt.Println(author3)  // 输出: Alan Mycroft

	// 获取第2章的标题和页码
	chapter2Title, _ := easyJSON.GetString("chapters[1].title")
	fmt.Println(chapter2Title) // 输出: Basic Go
	chapter2Pages, _ := easyJSON.GetInt64("chapters[1].pages")
	fmt.Println(chapter2Pages) // 输出: 33

	/*************************************
	生成JSON字符串
	使用EasyJSON.Array()生成数组
	使用EasyJSON.Object()生成对象
	 *************************************/
	bookObject := EasyJSON.Object(
		"name", "Go in Action",
			"price", 138.27,
			"authors", EasyJSON.Array("Raoul-Gabriel Urma", "Mario Fusco", "Alan Mycroft"),
			"chapters", EasyJSON.Array(
				EasyJSON.Object("title", "Introduction", "pages", 22),
				EasyJSON.Object("title", "Basic Go", "pages", 33)),
			"pages", 550)

	fmt.Println(bookObject.String())
	/*
	输出:
	{
		"name": "Go in Action",
		"price": 138.27,
		"authors": ["Raoul-Gabriel Urma", "Mario Fusco", "Alan Mycroft"],
		"chapters": [{
			"title": "Introduction",
			"pages": 22
		}, {
			"title": "Basic Go",
			"pages": 33
		}],
		"pages": 550
	}
	 */
}

Documentation

Index

Constants

View Source
const (
	JSON_TYPE_INVALID = 0 // 0 -- 无效的JSON类型
	JSON_TYPE_OBJECT  = 1 // 1 -- JSON对象
	JSON_TYPE_ARRAY   = 2 // 2 -- JSON数组
)

Variables

View Source
var (
	ErrInvalidJSONString = errors.New("invalid JSON string")
	ErrInvalidArguments  = errors.New("invalid arguments")
	ErrIndexOutOfBounds  = errors.New("index out of bounds")
	ErrFieldNotExists    = errors.New("field not exists")
	ErrNotAnArray        = errors.New("not an array")
	ErrNotAnObject       = errors.New("not an object")
	ErrNotAStruct        = errors.New("not a struct")
)

Functions

func Stringer

func Stringer(s string, escapeHTML bool) string

NOTE: keep in sync with stringBytes below.

Types

type EasyJSON

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

func Array

func Array(args ...interface{}) *EasyJSON

* 生成一个JSON数组 参数可以是任意类型

func Object

func Object(args ...interface{}) *EasyJSON

* 生成一个JSON对象 第0个参数为name,第1个参数为value,第2个参数为name,第3个参数为value,... 依此类推 name必须为string类型,value可以为任意类型

func Parse

func Parse(jsonString string) (*EasyJSON, error)

从给定的jsonString解析出EasyJSON对象 返回

如果成功,返回EasyJSON的指针,并且error为nil
如果失败,EasyJSON的指针的指针为nil,error为具体的报错信息

func (*EasyJSON) Append

func (easyJSON *EasyJSON) Append(path string, value interface{}) error

func (*EasyJSON) Exists

func (easyJSON *EasyJSON) Exists(path string) bool

func (*EasyJSON) Get

func (easyJSON *EasyJSON) Get(path string) (interface{}, error)

func (*EasyJSON) GetArray

func (easyJSON *EasyJSON) GetArray(path string) (*EasyJSON, error)

func (*EasyJSON) GetBoolean

func (easyJSON *EasyJSON) GetBoolean(path string) (bool, error)

func (*EasyJSON) GetData

func (easyJSON *EasyJSON) GetData() interface{}

* 获取底层的JSON数据表示

func (*EasyJSON) GetFloat64

func (easyJSON *EasyJSON) GetFloat64(path string) (float64, error)

func (*EasyJSON) GetInt64

func (easyJSON *EasyJSON) GetInt64(path string) (int64, error)

func (*EasyJSON) GetJSONType

func (easyJSON *EasyJSON) GetJSONType() int

* 获取EasyJSON的类型 返回:

1 -- JSON_TYPE_OBJECT JSON对象
2 -- JSON_TYPE_ARRAY  JSON数组

func (*EasyJSON) GetObject

func (easyJSON *EasyJSON) GetObject(path string) (*EasyJSON, error)

func (*EasyJSON) GetString

func (easyJSON *EasyJSON) GetString(path string) (string, error)

func (*EasyJSON) Length

func (easyJSON *EasyJSON) Length() int

获取EasyJSONObect 或 EasyJSONArray的元素个数

func (*EasyJSON) Opt

func (easyJSON *EasyJSON) Opt(path string, defaultValue interface{}) interface{}

func (*EasyJSON) OptArray

func (easyJSON *EasyJSON) OptArray(path string, defaultValue *EasyJSON) *EasyJSON

func (*EasyJSON) OptBoolean

func (easyJSON *EasyJSON) OptBoolean(path string, defaultValue bool) bool

func (*EasyJSON) OptFloat64

func (easyJSON *EasyJSON) OptFloat64(path string, defaultValue float64) float64

func (*EasyJSON) OptInt64

func (easyJSON *EasyJSON) OptInt64(path string, defaultValue int64) int64

func (*EasyJSON) OptObject

func (easyJSON *EasyJSON) OptObject(path string, defaultValue *EasyJSON) *EasyJSON

func (*EasyJSON) OptString

func (easyJSON *EasyJSON) OptString(path string, defaultValue string) string

func (*EasyJSON) Range

func (easyJSON *EasyJSON) Range(callback func(key interface{}, value interface{}))

遍历EasyJSONObect 或 EasyJSONArray callback: 回调函数 如果是EasyJSONObect,key的类型为string;如果是EasyJSONArray,key的类型为int;

func (*EasyJSON) Set

func (easyJSON *EasyJSON) Set(path string, value interface{}) error

func (*EasyJSON) String

func (easyJSON *EasyJSON) String() string

返回JSON字符串 因为 var arr []interface{} json.Marshal(arr) 会返回null 所以要自定义String()方法

json/encode.go源码中的注释 Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.

Jump to

Keyboard shortcuts

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