GithubHelp home page GithubHelp logo

winterssy / gjson Goto Github PK

View Code? Open in Web Editor NEW
7.0 0.0 2.0 11 KB

Make it easy to read arbitrary JSON in Go

Home Page: https://pkg.go.dev/github.com/winterssy/gjson

License: MIT License

Go 100.00%
golang go json jsoniter

gjson's Introduction

gjson

gjson provides a convenient way to read arbitrary JSON in Go.

Build codecov Go Report Card GoDoc License

Install

go get -u github.com/winterssy/gjson

Usage

import "github.com/winterssy/gjson"

Quick Start

  • Parse to gjson.Object
data, err := ioutil.ReadFile("./testdata/music.json")
if err != nil {
    log.Fatal(err)
}

obj, err := gjson.Parse(data)
if err != nil {
    panic(err)
}

fmt.Println(obj.GetNumber("code"))
fmt.Println(obj.GetString("data", "total"))
fmt.Println(obj.GetArray("data", "list").Index(0).ToObject().GetString("name"))
// Output:
// 200
// 1917
// 告白气球
  • Bind to struct
const dummyData = `
{
  "code": 200,
  "data": {
    "list": [
      {
        "artist": "周杰伦",
        "album": "周杰伦的床边故事",
        "name": "告白气球"
      },
      {
        "artist": "周杰伦",
        "album": "说好不哭 (with 五月天阿信)",
        "name": "说好不哭 (with 五月天阿信)"
      }
    ]
  }
}
`
var s struct {
    Code int `json:"code"`
    Data struct {
        List gjson.Array `json:"list"`
    } `json:"data"`
}
err := gjson.UnmarshalFromString(dummyData, &s)
if err != nil {
    panic(err)
}

fmt.Println(s.Data.List.Index(0).ToObject().GetString("name"))
// Output:
// 告白气球

Build with jsoniter

gjson uses encoding/json as default json package but you can change to jsoniter by build from other tags.

go build -tags=jsoniter .

License

MIT

gjson's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

lhmwzy xz0609

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.