Git Product home page Git Product logo

jsonext's Introduction

jsonext to make json handle more ok

simple use

package main

import (
    "fmt"
    "github.com/jeppeter/jsonext"
    "os"
)

func main() {
    var vmap map[string]interface{}
    var err error
    msg := `
    {
        "path"  : "new",
        "cc" : "www",
        "zz" : {
            "path" : {
                "hello" : "world"
            },
            "cc" : {
                "bon" : "jour"
            }
        }
    }
    `
    vmap, err = jsonext.GetJsonMap(msg)
    if err != nil {
        fmt.Fprintf(os.Stderr, "can not parse [%s] error[%s]\n", msg, err.Error())
        os.Exit(5)
    }
    for k, v := range vmap {
        switch v.(type) {
        case string:
            fmt.Printf("[%s]=[%s]\n", k, v.(string))
        }
    }
    return
}

run command

go build -o simple simple.go
./simple

output

[path]=[new]
[cc]=[www]

more compound example

package main

import (
    "fmt"
    "github.com/jeppeter/jsonext"
    "io"
    "os"
)

func DumpAMap(key string, amap []interface{}, level int, writer io.Writer) error {
    var icnt int
    for icnt = 0; icnt < len(amap); icnt++ {
        v := amap[icnt]
        switch v.(type) {
        case string:
            s := fmt.Sprintf("level[%d][%s][%d]=[%s]\n", level, key, icnt, v.(string))
            writer.Write([]byte(s))
        case float64:
            s := fmt.Sprintf("level[%d][%s][%d]=[%f]\n", level, key, icnt, v.(float64))
            writer.Write([]byte(s))
        case int:
            s := fmt.Sprintf("level[%d][%s][%d]=[%d]\n", level, key, icnt, v.(int))
            writer.Write([]byte(s))
        case int64:
            s := fmt.Sprintf("level[%d][%s][%d]=[%d]\n", level, key, icnt, v.(int64))
            writer.Write([]byte(s))
        case float32:
            s := fmt.Sprintf("level[%d][%s][%d]=[%f]\n", level, key, icnt, v.(float32))
            writer.Write([]byte(s))
        case map[string]interface{}:
            DumpVMap(v.(map[string]interface{}), level+1, writer)
        case []interface{}:
            DumpAMap(fmt.Sprintf("[%s][%d]", key, icnt), v.([]interface{}), level+1, writer)
        }
    }
    return nil
}

func DumpVMap(vmap map[string]interface{}, level int, writer io.Writer) error {
    for k, v := range vmap {
        switch v.(type) {
        case string:
            s := fmt.Sprintf("level[%d][%s]=[%s]\n", level, k, v.(string))
            writer.Write([]byte(s))
        case float64:
            s := fmt.Sprintf("level[%d][%s]=[%f]\n", level, k, v.(float64))
            writer.Write([]byte(s))
        case int:
            s := fmt.Sprintf("level[%d][%s]=[%d]\n", level, k, v.(int))
            writer.Write([]byte(s))
        case int64:
            s := fmt.Sprintf("level[%d][%s]=[%d]\n", level, k, v.(int64))
            writer.Write([]byte(s))
        case float32:
            s := fmt.Sprintf("level[%d][%s]=[%f]\n", level, k, v.(float32))
            writer.Write([]byte(s))
        case map[string]interface{}:
            DumpVMap(v.(map[string]interface{}), level+1, writer)
        case []interface{}:
            DumpAMap(k, v.([]interface{}), level+1, writer)
        default:
            s := fmt.Sprintf("level[%d][%s]=[%v]\n", level, k, v)
            writer.Write([]byte(s))
        }
    }
    return nil
}

func main() {
    var vmap map[string]interface{}
    var err error
    var vs string
    msg := `
    {
        "path"  : "new",
        "cc" : "www",
        "zz" : {
            "path" : {
                "hello" : "world"
            },
            "cc" : {
                "bon" : "jour"
            }
        },
        "array" : ["ccc",20 ,333.90]
    }
    `
    vmap, err = jsonext.GetJsonMap(msg)
    if err != nil {
        fmt.Fprintf(os.Stderr, "can not parse [%s] error[%s]\n", msg, err.Error())
        os.Exit(5)
    }
    DumpVMap(vmap, 0, os.Stdout)
    vs, err = jsonext.GetJsonValue("zz/path/non", vmap)
    if err != nil {
        fmt.Fprintf(os.Stderr, "can not get zz/path/non [%s]\n", err.Error())
    }
    vs, err = jsonext.GetJsonValue("zz/path/hello", vmap)
    fmt.Fprintf(os.Stdout, "get [zz/path/hello]=[%s]\n", vs)
    vs, err = jsonext.GetJsonValue("array", vmap)
    fmt.Fprintf(os.Stdout, "get [array]=[%s]\n", vs)
    return
}

run command

go build -o compound compound.go
./compound

output

level[0][path]=[new]
level[0][cc]=[www]
level[2][hello]=[world]
level[2][bon]=[jour]
level[1][array][0]=[ccc]
level[1][array][1]=[20.000000]
level[1][array][2]=[333.900000]
can not get zz/path/non [can not find (non) in zz/path/non]
get [zz/path/hello]=[world]
get [array]=[[ "ccc", 20, 333.900000]]

jsonext's People

Contributors

jeppeter avatar

Watchers

James Cloos avatar  avatar  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.