Simplejson Go Save

【Deprecated】This repo has moved to: https://github.com/likexian/gokit/tree/master/xjson

Project 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

Open Source Agenda is not affiliated with "Simplejson Go" Project. README Source: likexian/simplejson-go
Stars
25
Open Issues
0
Last Commit
4 years ago
License

Open Source Agenda Badge

Open Source Agenda Rating