json

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 7 Imported by: 10

README

go-json

go-json created to ease json parsing and building without hasle using struct.

Install

go get -u github.com/eqto/go-json

Example

1. Json Parsing

Format JSON string and will returns the JSON Struct.

Parameters :

jsonBytes - it should be []byte format that contain JSON

Returns :

JSON struct

How to use

/*
data:
{
  "title": "Learning go",
  "num_chapters": 10,
  "author": {
    "first_name": "John",
    "last_name": "Doe"
  }
}
*/

obj := json.Parse(data)

fmt.println(obj.GetString(`title`))         //print: Learning go

fmt.println(obj.GetInt(`num_chapters`))   //print: 10

fmt.println(obj.GetString(`author.first_name`))   //print: John

2. GetJsonArray

Returns the array value to which the specified name is mapped.

Returns :

array value

How to use

/*
data:
{
  "books": [
    {
      "title": "Learning go",
      "num_chapters": 10
    },
    {
      "title": "Basic of go",
      "num_chapters": 5
    }
  ]
}
*/

obj := goson.Parse(data)

arr := obj.GetArray(`books`)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

func Unmarshal added in v0.5.0

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

Types

type Beautifier added in v0.5.0

type Beautifier interface {
	String() string
	Bytes() []byte
}

type Object

type Object map[string]interface{}

func Parse

func Parse(data []byte) (Object, error)

func ParseArray

func ParseArray(data []byte) ([]Object, error)

func ParseFile

func ParseFile(filename string) (Object, error)

func ParseObject

func ParseObject(data interface{}) (Object, error)

func ParseString

func ParseString(data string) (Object, error)

func (Object) Array added in v0.2.2

func (j Object) Array(path string) []interface{}

func (Object) Beautify added in v0.5.0

func (j Object) Beautify() Beautifier

func (Object) Bytes added in v0.5.0

func (j Object) Bytes() []byte

func (Object) Clone added in v0.2.1

func (j Object) Clone() Object

func (Object) CopyTo added in v0.4.0

func (j Object) CopyTo(js *Object)

func (Object) Get

func (j Object) Get(path string) interface{}

func (Object) GetArray

func (j Object) GetArray(path string) []Object

func (Object) GetBoolean

func (j Object) GetBoolean(path string) bool

func (Object) GetBooleanNull

func (j Object) GetBooleanNull(path string) *bool

func (Object) GetBooleanOr

func (j Object) GetBooleanOr(path string, defValue bool) bool

func (Object) GetFloat

func (j Object) GetFloat(path string) float64

func (Object) GetFloatNull

func (j Object) GetFloatNull(path string) *float64

func (Object) GetFloatOr

func (j Object) GetFloatOr(path string, defValue float64) float64

func (Object) GetInt

func (j Object) GetInt(path string) int

func (Object) GetIntArray

func (j Object) GetIntArray(path string) []int

func (Object) GetIntNull

func (j Object) GetIntNull(path string) *int

func (Object) GetIntOr

func (j Object) GetIntOr(path string, defValue int) int

func (Object) GetJSONObject

func (j Object) GetJSONObject(path string) Object

func (Object) GetString

func (j Object) GetString(path string) string

func (Object) GetStringArray

func (j Object) GetStringArray(path string) []string

func (Object) GetStringNull

func (j Object) GetStringNull(path string) *string

func (Object) GetStringOr

func (j Object) GetStringOr(path string, defValue string) string

func (Object) Has added in v0.2.0

func (j Object) Has(key string) bool

func (Object) Put

func (j Object) Put(path string, value interface{}) Object

func (Object) Remove

func (j Object) Remove(path string)

func (Object) String added in v0.5.0

func (j Object) String() string

Jump to

Keyboard shortcuts

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