Help us shape the future of Go! Take the 2026 Go Developer Survey

simplejson

package module
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2019 License: Apache-2.0 Imports: 8 Imported by: 27

README

simplejson.go

License GoDoc Build Status Go Report Card Code Cover

simplejson-go is a simple and powerful Go module for JSON parsing.

Overview

This module provides the most friendly way to use JSON in golang.

Features

  • Easy load to json and dump to string
  • Load and dump with file is supported
  • Modify the json data is simple
  • One line retrieval with MustXXX
  • Get by dot notation key is supported

Installation

go get -u github.com/likexian/simplejson-go

Importing

import (
    "github.com/likexian/simplejson-go"
)

Documentation

Visit the docs on GoDoc

Example

Dump the struct data to JSON string
// Define Status struct
type Status struct {
    Code    int64  `json:"code"`
    Message string `json:"message"`
}

// Init status
status := Status{1, "Success"}

// Dump status to json string
j := simplejson.New(status)
s, err := j.Dumps()
if err == nil {
    fmt.Println("Json text is:", s)
}

// OR dumps using the easy way
s, err := simplejson.Dumps(status)
if err == nil {
    fmt.Println("Json text is:", s)
}
Dump the map data to JSON string
// Init a map data
data := map[string]interface{}{
    "code": 1,
    "message": "success",
    "result": {
        "Name": "Li Kexian"
    }
}

// Dump to string in the easy way
s, err := simplejson.Dumps(status)
if err == nil {
    fmt.Println("Json text is:", s)
}
Load the JSON string
// Json strig
text := `{"Code": 1, "Message": "Success", "Result": {"Student": [{"Name": "Li Kexian"}]}}`

// Load json string
j, err := simplejson.Loads(text)
if err == nil {
    fmt.Println("Code is:", j.Get("Code").MustInt(0))
    fmt.Println("Message is:", j.Get("Message").MustString(""))
    fmt.Println("First Student name is:", j.Get("Result.Student.0.Name").MustString("-"))
}

LICENSE

Copyright 2012-2019 Li Kexian

Licensed under the Apache License 2.0

About

DONATE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Author

func Author() string

Author returns package author

func Dump

func Dump(path string, data interface{}) error

Dump dumps json object to a file

func Dumps

func Dumps(data interface{}) (string, error)

Dumps marshal json object to string

func License

func License() string

License returns package license

func PrettyDumps

func PrettyDumps(data interface{}) (string, error)

PrettyDumps marshal json object to string, with identation

func Version

func Version() string

Version returns package version

Types

type Json

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

Json storing json data

func Load

func Load(path string) (*Json, error)

Load loads data from file, returns a json object

func Loads

func Loads(text string) (*Json, error)

Loads unmarshal json from string, returns json object

func New

func New(args ...interface{}) *Json

New returns a pointer to a new Json object

data_json := New()
data_json := New(type Data struct{data string}{"zzz"})
data_json := New(map[string]interface{}{"iam": "Li Kexian"})

func (*Json) Array

func (j *Json) Array() (result []interface{}, err error)

Array returns as array from json object

func (*Json) Bool

func (j *Json) Bool() (result bool, err error)

Bool returns as bool from json object

func (*Json) Del

func (j *Json) Del(key string)

Del delete key-value from json object, dot(.) separated key is supported

json.Del("status")
json.Del("status.code")
! NOT SUPPORTED json.Del("result.intlist.3")

func (*Json) Dump

func (j *Json) Dump(path string) (err error)

Dump dumps json object to a file

func (*Json) Dumps

func (j *Json) Dumps() (result string, err error)

Dumps marshal json object to string

func (*Json) Float64

func (j *Json) Float64() (result float64, err error)

Float64 returns as float64 from json object

func (*Json) Get

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

Get returns the pointer to json object by key, dot(.) separated key is supported

json.Get("status").Int()
json.Get("status.code").Int()
json.Get("result.intlist.3").Int()

func (*Json) Has

func (j *Json) Has(key string) bool

Has check json object has key, dot(.) separated key is supported

json.Has("status")
json.Has("status.code")
json.Has("result.intlist.3")

func (*Json) Index

func (j *Json) Index(i int) *Json

Index returns a pointer to the index of json object

json.Get("int_list").Index(1).Int()

func (*Json) Int

func (j *Json) Int() (result int, err error)

Int returns as int from json object

func (*Json) Int64

func (j *Json) Int64() (result int64, err error)

Int64 returns as int64 from json object

func (*Json) IsArray

func (j *Json) IsArray() bool

IsArray returns json object is an array

func (*Json) IsMap

func (j *Json) IsMap() bool

IsMap returns json object is a map

func (*Json) Len

func (j *Json) Len() int

Len returns len of json object, -1 if type invalid or error

func (*Json) Load

func (j *Json) Load(path string) error

Load loads data from file, returns a json object

func (*Json) Loads

func (j *Json) Loads(text string) error

Loads unmarshal json from string, returns json object

func (*Json) Map

func (j *Json) Map() (result map[string]interface{}, err error)

Map returns as map from json object

func (*Json) MustArray

func (j *Json) MustArray(args ...[]interface{}) []interface{}

MustArray returns as array from json object with optional default value if error return default(if set) or panic

func (*Json) MustBool

func (j *Json) MustBool(args ...bool) bool

MustBool returns as bool from json object with optional default value if error return default(if set) or panic

func (*Json) MustFloat64

func (j *Json) MustFloat64(args ...float64) float64

MustFloat64 returns as float64 from json object with optional default value if error return default(if set) or panic

func (*Json) MustInt

func (j *Json) MustInt(args ...int) int

MustInt returns as int from json object with optional default value if error return default(if set) or panic

func (*Json) MustInt64

func (j *Json) MustInt64(args ...int64) int64

MustInt64 returns as int64 from json object with optional default value if error return default(if set) or panic

func (*Json) MustMap

func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{}

MustMap returns as map from json object with optional default value if error return default(if set) or panic

func (*Json) MustString

func (j *Json) MustString(args ...string) string

MustString returns as string from json object with optional default value if error return default(if set) or panic

func (*Json) MustStringArray

func (j *Json) MustStringArray(args ...[]string) []string

MustStringArray returns as string from json object with optional default value if error return default(if set) or panic

func (*Json) MustTime

func (j *Json) MustTime(args ...interface{}) time.Time

MustTime returns as time.Time from json object if error return default(if set) or panic

json.Time()                                                 // No format,  No default
json.Time("2006-01-02 15:04:05")                            // Has format, No default
json.Time(time.Unix(1548907870, 0))                         // No format,  Has default
json.Time("2006-01-02 15:04:05", time.Unix(1548907870, 0))  // Has format, Has default

func (*Json) MustUint64

func (j *Json) MustUint64(args ...uint64) uint64

MustUint64 returns as uint64 from json object with optional default value if error return default(if set) or panic

func (*Json) PrettyDumps

func (j *Json) PrettyDumps() (result string, err error)

PrettyDumps marshal json object to string, with identation

func (*Json) Set

func (j *Json) Set(key string, value interface{})

Set set key-value to json object, dot(.) separated key is supported

json.Set("status", 1)
json.Set("status.code", 1)
! NOT SUPPORTED json.Set("result.intlist.3", 666)

func (*Json) SetHtmlEscape

func (j *Json) SetHtmlEscape(escape bool)

SetHtmlEscape set html escape for escaping of <, >, and & in JSON strings

func (*Json) String

func (j *Json) String() (result string, err error)

String returns as string from json object

func (*Json) StringArray

func (j *Json) StringArray() (result []string, err error)

StringArray returns as string array from json object

func (*Json) Time

func (j *Json) Time(args ...string) (result time.Time, err error)

Time returns as time.Time from json object optional args is to set the time string parsing format, time.RFC3339 by default if the time is of int, optional args must not set

json.Time()
json.Time("2006-01-02 15:04:05")

func (*Json) Uint64

func (j *Json) Uint64() (result uint64, err error)

Uint64 returns as uint64 from json object

Jump to

Keyboard shortcuts

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