gjson

package module
v0.0.0-...-ace0184 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2020 License: Apache-2.0 Imports: 5 Imported by: 5

README

Fastjson-based secondary packaging

It's easy to construct and parse JSON How to use

Getting Started

Installing

To start using GJSON, install Go and run go get:

$ go get -u github.com/yushaona/gjson

How to use

Construct JSON
  • Test code
package main

import (
	"fmt"
	
	"github.com/yushaona/gjson"
)
func main() {
	var main gjson.GJSON
	main.SetInt("BigIntSupported", 9223372036854775807)
	main.SetString("date", "20180322")
	main.SetString("city", "北京")

	var data gjson.GJSON
	data.SetFloat64("pm25", 73.5)
	data.SetString("ganmao", "极少数敏感人群应减少户外活动")
	var yesterday gjson.GJSON
	yesterday.SetString("date", "21日星期三")
	yesterday.SetInt("aqi", 85)
	data.SetObject("yesterday", yesterday)
	main.SetObject("data", data)

	var forecast gjson.GJSON
	item1 := forecast.AddItem()
	item1.SetInt("aqi", 98)
	item1.SetString("notice", "愿你拥有比阳光明媚的心情")
	item2 := forecast.AddItem()
	item2.SetInt("aqi", 118)
	item2.SetString("notice", "阴晴之间,谨防紫外线侵扰")
	main.SetArray("forecast", forecast)
	fmt.Println(main.ToString())
}
  • Output
{
    "BigIntSupported": 9223372036854775807,
    "date": "20180322",
    "city": "北京",
    "data": {
        "pm25": 73.5,
        "ganmao": "极少数敏感人群应减少户外活动",
        "yesterday": {
            "date": "21日星期三",
            "aqi": 85
        }
    },
    "forecast": [
        {
            "aqi": 98,
            "notice": "愿你拥有比阳光明媚的心情"
        },
        {
            "aqi": 118,
            "notice": "阴晴之间,谨防紫外线侵扰"
        }
    ]
}
parse JSON
  • Test code
package main

import (
	"fmt"
	
	"github.com/yushaona/gjson"
)
func main() {
	var main gjson.GJSON
	err := main.Load(`{"BigIntSupported":9223372036854775807,"date":"20180322","city":"北京","data":{"pm25":73.5,"ganmao":"极少数敏感人群应减少户外活动","yesterday":{"date":"21日星期三","aqi":85}},"forecast":[{"aqi":98,"notice":"愿你拥有比阳光明媚的心情"},{"aqi":118,"notice":"阴晴之间,谨防紫外线侵扰"}]}`)

	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Printf("%s:%d\n", "BigIntSupported", main.GetInt64("BigIntSupported"))
		fmt.Printf("%s:%s\n", "date", main.GetString("date"))
		fmt.Printf("%s:%d\n", "date", main.GetInt64("date"))
		fmt.Printf("%s:%s\n", "city", main.GetString("city"))

		data := main.GetObject("data")
		fmt.Printf("%s:%f\n", "pm25", data.GetFloat64("pm25"))
		fmt.Printf("%s:%s\n", "ganmao", data.GetString("ganmao"))
		yesterday := data.GetObject("yesterday")
		fmt.Printf("%s:%s\n", "date", yesterday.GetString("date"))
		fmt.Printf("%s:%d\n", "aqi", yesterday.GetInt("aqi"))

		forecast := main.GetArray("forecast")
		num := forecast.ItemCount()

		for i := 0; i < num; i++ {
			item := forecast.Item(i)
			fmt.Printf("%s:%d\n", "aqi", item.GetInt("aqi"))
			fmt.Printf("%s:%s\n", "notice", item.GetString("notice"))
		}

	}
}

  • Output
BigIntSupported:9223372036854775807
date:20180322
date:20180322
city:北京
pm25:73.500000
ganmao:极少数敏感人群应减少户外活动
date:21日星期三
aqi:85
aqi:98
notice:愿你拥有比阳光明媚的心情
aqi:118
notice:阴晴之间,谨防紫外线侵扰

Documentation

Index

Constants

View Source
const (
	TypeObject
	TypeArray
)

Variables

This section is empty.

Functions

This section is empty.

Types

type GJSON

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

GJSON struct

func NewGJSON

func NewGJSON(t GjsonType) *GJSON

func (*GJSON) AddItem

func (t *GJSON) AddItem() (result *GJSON)

AddItem 添加一个子对象

func (*GJSON) GetArray

func (t *GJSON) GetArray(k string) (result *GJSON)

func (*GJSON) GetBool

func (t *GJSON) GetBool(k string) bool

GetBool only fastjson.TypeBool Type get bool value return correct

func (*GJSON) GetBytes

func (t *GJSON) GetBytes(k string) []byte

func (*GJSON) GetFloat64

func (t *GJSON) GetFloat64(k string) float64

func (*GJSON) GetInt

func (t *GJSON) GetInt(k string) int

func (*GJSON) GetInt64

func (t *GJSON) GetInt64(k string) int64

func (*GJSON) GetObject

func (t *GJSON) GetObject(k string) (result *GJSON)

func (*GJSON) GetString

func (t *GJSON) GetString(k string) string

GetString get string value

func (*GJSON) GetTime

func (t *GJSON) GetTime(k string) (result time.Time)

func (*GJSON) Interface

func (t *GJSON) Interface() interface{}

func (*GJSON) IsExist

func (t *GJSON) IsExist(k string) bool

func (*GJSON) Item

func (t *GJSON) Item(index int) (result *GJSON)

func (*GJSON) ItemCount

func (t *GJSON) ItemCount() (num int)

func (*GJSON) Load

func (t *GJSON) Load(s string) (err error)

Load load string

func (*GJSON) SetArray

func (t *GJSON) SetArray(k string, v GJSON)

func (*GJSON) SetBool

func (t *GJSON) SetBool(k string, v bool)

////////////////////////////////////////////////Set*/////////////////////////

func (*GJSON) SetBytes

func (t *GJSON) SetBytes(k string, v []byte)

func (*GJSON) SetFloat64

func (t *GJSON) SetFloat64(k string, v float64)

func (*GJSON) SetInt

func (t *GJSON) SetInt(k string, v int)

func (*GJSON) SetObject

func (t *GJSON) SetObject(k string, v GJSON)

func (*GJSON) SetString

func (t *GJSON) SetString(k, v string)

func (*GJSON) ToString

func (t *GJSON) ToString() string

type GjsonType

type GjsonType int

Jump to

Keyboard shortcuts

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