ljson

package module
v0.0.0-...-4d086f9 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: MIT Imports: 1 Imported by: 0

README

ljson

介绍

一个模仿cjson的json go语言库

安装

go get gitee.com/lutianming/ljson

Example
package main

import "gitee.com/lutianming/ljson"

func main() {
		var data string = "  {  \"a\"   :   \"b\"  ,  \"c\"   :  5  ,  \"d\"  :  true  ,   \"e\"   :  {  \r\n \"f\"  :  {  \"g\"  :  \"h\",  \"i\":[{\"ab\":\"cd\"}, 6, 8]  }  }  }"
	//var data string = "  {    \"e\"   :  {   \"f\"  :  5 }  }"
	
	/*字符串解析为json object*/
	root := ljson.Parse(data)
	if root != nil {
		print("sucess\n")
	} else {
		print("failed\n")
	}
	item := root.ObjectGetItem("c")
	if item.IsNumber() {
		print("c:", item.Valuenumber, "\n")
	}
	/*json object编码为字符串*/
	encodestr := root.String()
	print("\nencodestr:", encodestr, "\n")
	encodebytes, n := root.Bytes()
	print("\nencodebytes:", string(encodebytes[:n]), "\n")

	/*创建json object*/
	root = ljson.CreateObject()
	root.ObjectAddNumber("abc", 6548)
	root.ObjectAddString("def", "afwefefe")
	root.ObjectAddBool("flag", true)

	/*创建json array*/
	array := ljson.CreateArray()
	root.ObjectAddItem("dict", array)
	array.ArrayAddString("54646")
	array.ArrayAddNumber(3654654)

	array.ArrayAddItem(ljson.CreateObject())
	array.ArrayAddItem(ljson.CreateArray())
	encodestr = root.String()
	print("\nencodestr:", encodestr, "\n")

	/*遍历array,并且可用于遍历object*/
	//第一种方法,效率低
	print("first\n")
	size := array.GetSize()
	for i := 0; i < size; i++ {
		item = array.ArrayGetItem(i)
		print(item.String(), "\n")
	}

	//第二种方法,相对第一种方法效率高
	print("second\n")
	var iter *ljson.Json = nil
	for {
		iter = array.ItemNext(iter)
		if iter != nil {
			print(iter.String(), "\n")
		} else {
			break
		}

	}
}

Documentation

Index

Constants

View Source
const (
	Json_Invalid = 0
	Json_NIL     = 1 << 0
	Json_Bool    = 1 << 1
	Json_Number  = 1 << 2
	Json_String  = 1 << 3
	Json_Array   = 1 << 4
	Json_Object  = 1 << 5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Json

type Json struct {
	Type int8

	Valuebool   bool
	Valuestring string
	Valuenumber int64
	Valuefloat  float64

	Name string
	// contains filtered or unexported fields
}

func CreateArray

func CreateArray() *Json

func CreateBoolObject

func CreateBoolObject(value bool) *Json

func CreateNumberObject

func CreateNumberObject(value int64) *Json

func CreateObject

func CreateObject() *Json

func CreateStringObject

func CreateStringObject(value string) *Json

func ParseBytes

func ParseBytes(bytes []byte, n int) *Json

func ParseString

func ParseString(str string) *Json

func (*Json) ArrayAddBool

func (node *Json) ArrayAddBool(value bool)

func (*Json) ArrayAddItem

func (node *Json) ArrayAddItem(item *Json)

func (*Json) ArrayAddNumber

func (node *Json) ArrayAddNumber(value int64)

func (*Json) ArrayAddString

func (node *Json) ArrayAddString(value string)

func (*Json) ArrayGetItem

func (node *Json) ArrayGetItem(index int) *Json

func (*Json) Bytes

func (node *Json) Bytes() ([]byte, int)

func (*Json) GetSize

func (node *Json) GetSize() int

func (*Json) IsArray

func (node *Json) IsArray() bool

func (*Json) IsBool

func (node *Json) IsBool() bool

func (*Json) IsNumber

func (node *Json) IsNumber() bool

func (*Json) IsObject

func (node *Json) IsObject() bool

func (*Json) IsString

func (node *Json) IsString() bool

func (*Json) ItemNext

func (node *Json) ItemNext(iter *Json) *Json

func (*Json) ObjectAddBool

func (node *Json) ObjectAddBool(key string, value bool)

func (*Json) ObjectAddItem

func (node *Json) ObjectAddItem(key string, item *Json)

func (*Json) ObjectAddNumber

func (node *Json) ObjectAddNumber(key string, value int64)

func (*Json) ObjectAddString

func (node *Json) ObjectAddString(key string, value string)

func (*Json) ObjectGetItem

func (node *Json) ObjectGetItem(key string) *Json

func (*Json) String

func (node *Json) String() string

Jump to

Keyboard shortcuts

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