gojson

package module
v0.0.0-...-653aded Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: GPL-3.0 Imports: 6 Imported by: 2

README

gojson

a Go package to interact with arbitrary JSON

  1. Construct json object by API;
  2. Decode json string, then Get the target value;
  3. Function is simple and easy;
package main

import (
	"fmt"
	"time"

	"github.com/gofish2020/gojson"
)

func main() {

    //construct json
	var js gojson.Json
	js.Set("ints").SetAny(100)                           //整数
	js.Set("strs").SetAny("T")                           //字符串
	js.Set("floats").SetAny(3.1415926)                   //浮点数
	js.Set("timestamp").SetAny(time.Now())               //时间戳
	js.Set("timestampstr").SetAny("2009-01-02 15:02:01") //字符串表示时间
	js.Set("boolen").SetAny(true)                        //布尔
	js.Set("array").AddIndex().SetAny(1)
	js.Set("array").AddIndex().SetAny("2")
	fmt.Println("数组长度:", js.Get("array").ArrayLen())
	//遍历数组
	for i := 0; i < js.Get("array").ArrayLen(); i++ {
		fmt.Println(js.Get("array").GetIndex(i).String())
	}
    //load json string  and set loadstr
	js.Set("loadstr").LoadString(`{"nash":111,"ss":1.2,"bb":"23"}`) //用字符串设定loadstr的值
	fmt.Println("///////////////////////")

    //one json object set other json object
	var newJs gojson.Json
	newJs.SetAny(js.Interface()) //用json对象设置值

	var newJs1 gojson.Json
	newJs1.Set("copyJs").SetAny(js.Interface())

    // json object to json string
	jsStr1, err1 := newJs1.Encode()
	fmt.Println("------>>>", string(jsStr1), err1)


    // get key's different type value
	fmt.Println("将js对象转化json字符串///////////////////////")
	jsStr, err := js.Encode()
	fmt.Println("======>>>", string(jsStr), err)
	fmt.Println("转化为字符串类型:///////////////////////")
	fmt.Println(js.Get("ints").String())
	fmt.Println(js.Get("strs").String())
	fmt.Println(js.Get("floats").String())
	fmt.Println(js.Get("timestamp").String()) //time类型转成字符串
	fmt.Println(js.Get("boolen").String())
	fmt.Println(js.Get("array").GetIndex(0).String())

	fmt.Println("转化为整数类型:///////////////////////")
	fmt.Println(js.Get("ints").Int())
	fmt.Println(js.Get("strs").Int())
	fmt.Println(js.Get("floats").Int())
	fmt.Println(js.Get("timestamp").Int())
	fmt.Println(js.Get("boolen").Int())
	fmt.Println("转化为布尔类型:///////////////////////")
	fmt.Println(js.Get("ints").Bool())
	fmt.Println(js.Get("strs").Bool())
	fmt.Println(js.Get("floats").Bool())
	fmt.Println(js.Get("timestamp").Bool())
	fmt.Println(js.Get("boolen").Bool())

	fmt.Println("转化为浮点数类型:///////////////////////")
	fmt.Println(js.Get("ints").Float64())
	fmt.Println(js.Get("strs").Float64())
	fmt.Println(js.Get("floats").Float64())
	fmt.Println(js.Get("timestamp").Float64())
	fmt.Println(js.Get("boolen").Float64())

	fmt.Println("转化为时间类型:///////////////////////")

	fmt.Println(js.Get("timestamp").Time())
	fmt.Println(js.Get("timestampstr").Time()) //字符串转成time类型

}

Documentation

Overview

author:nash date:2022/9/23 comment: a Go package to interact with arbitrary JSON

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Json

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

root 和 parent/key 是互斥的关系

func (*Json) AddIndex

func (t *Json) AddIndex() *Json

func (*Json) Array

func (t *Json) Array() []interface{}

func (*Json) ArrayLen

func (t *Json) ArrayLen() (count int)

func (*Json) Bool

func (t *Json) Bool() bool

作为 Bool 类型

func (*Json) Del

func (t *Json) Del(key string)

func (*Json) Encode

func (t *Json) Encode() (s []byte, err error)

func (*Json) EncodePretty

func (t *Json) EncodePretty() (s []byte, err error)

func (*Json) Float64

func (t *Json) Float64() float64

func (*Json) Get

func (t *Json) Get(key string) *Json

func (*Json) GetIndex

func (t *Json) GetIndex(i int) *Json

对数组进行操作

func (*Json) Int

func (t *Json) Int() int

作为 Int

func (*Json) Int64

func (t *Json) Int64() int64

作为 Int64

func (*Json) Interface

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

func (*Json) Kind

func (t *Json) Kind() reflect.Kind

func (*Json) Load

func (t *Json) Load(txt string) (err error)

func (*Json) LoadString

func (t *Json) LoadString(src string) (err error)

func (*Json) Map

func (t *Json) Map() map[string]interface{}

func (*Json) Nil

func (t *Json) Nil() bool

func (*Json) Set

func (t *Json) Set(key string) *Json

func (*Json) SetAny

func (t *Json) SetAny(i any)

设定任意值(和外部的变量脱离关系)

func (*Json) String

func (t *Json) String() string

作为字符串显示

func (*Json) Time

func (t *Json) Time() (res time.Time, err error)

func (*Json) Uint64

func (t *Json) Uint64() uint64

作为 Uint64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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