Git Product home page Git Product logo

simplejson-go's Introduction

simplejson.go

simplejson-go is a simple Go module for JSON parser.

Build Status

Overview

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

Installation

go get github.com/likexian/simplejson-go

Importing

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

Documentation

Get a new JSON

func New() (*Json)

Load string to JSON

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

Dump JSON to string

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

Dump JSON to string with pretty format

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

Load file to JSON

func Load(file string) (result *Json, err error)

Dump JSON to file

func Dump(file string, data *Json) (bytes int, err error)

Check key is in JSON

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

Get value from JSON

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

Set value to JSON

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

Get the map value

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

Get the array value

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

Get the bool value

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

Get the string value

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

Get the float64 value

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

Get the int value

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

Get the uint value

func (j *Json) Uint() (result uint, err error)

Get the int64 value

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

Get the uint64 value

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

Example

The struct for storing data

type Status struct {
    Code    int64  `json:"code"`
    Message string `json:"message"`
}

Initialize the struct and set value

data_status := Status{}
data_status.Code = 1
data_status.Message = "success"

Dump the struct data to JSON string

data_json := simplejson.Json{}
data_json.Data = data_status
data, err := simplejson.Dumps(&data_json)
if err == nil {
    fmt.Println(data)
}

Load the JSON string

json, err := Loads(data)
if err == nil {
    // Get the value of JSON
    fmt.Println(json.Get("Code").Int())
    fmt.Println(json.Get("Message").String())
}

LICENSE

Copyright 2012-2014, Kexian Li

Apache License, Version 2.0

About

Thanks

simplejson-go's People

Contributors

likexian avatar jsimnz avatar redfoxli avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.