simplejson-go is a simple and powerful Go module for JSON parsing.
This module provides the most friendly way to use JSON in golang.
- 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
go get -u github.com/likexian/simplejson-go
import (
"github.com/likexian/simplejson-go"
)
Visit the docs on GoDoc
// 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)
}
// 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)
}
// 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("-"))
}
Copyright 2012-2019 Li Kexian
Licensed under the Apache License 2.0