当前位置:   article > 正文

Golang go-simplejson_simplejson golang

simplejson golang

Source code

go-simplejson

a Go package to interact with arbitrary JSON.

Install

go get -u github.com/bitly/go-simplejson
  • 1

Import

import "github.com/bitly/go-simplejson"
  • 1

Demo

package main

import (
    "fmt"

    simplejson "github.com/bitly/go-simplejson"
)

func main() {
    js, err := simplejson.NewJson([]byte(`
  {
    "test": {
      "string_array": ["asdf", "ghjk", "zxcv"],
      "array": [1, "2", 3],
      "arraywithsubs": [{"subkeyone": 1}, {"subkeytwo": 2, "subkeythree": 3}],
      "int": 10,
      "float": 5.150,
      "bignum": 9223372036854775807,
      "string": "simplejson",
      "bool": true
    },
    "person": [
                {
                  "name": "piao",
                  "age": 30,
                  "email": "piaoyunsoft@163.com",
                  "phoneNum": ["13974999999","13984999999"]
                }, 
                {
                  "name": "aaaaa",
                  "age": 20,
                  "email": "aaaaaa@163.com",
                  "phoneNum": ["13974998888","13984998888"]
                }
    ]
  }
  `))
    if err != nil {
        panic("json format error")
    }
    fmt.Println("\n")

    // get some field
    fmt.Println("---------------------get some field-----------------")
    s, err := js.Get("test").Get("string").String()
    if err != nil {
        panic(err)
    }
    fmt.Println(s, "\n")

    // check existence of some field
    fmt.Println("---------------------check existence----------------")
    _, ok := js.Get("test").CheckGet("string2")
    if ok {
        fmt.Println("exist!")
    } else {
        fmt.Println("not exist!")
    }
    fmt.Println("\n")

    // array
    fmt.Println("---------------------array--------------------------")
    arr, err := js.Get("test").Get("string_array").StringArray()
    fmt.Println("# array of string")
    if err != nil {
        panic(err)
    }
    for _, v := range arr {
        fmt.Printf("%s\t", v)
    }
    fmt.Println("\n")

    fmt.Println("# array of number and string")
    arr2, err := js.Get("test").Get("array").Array()
    if err != nil {
        panic(err)
    }
    for _, v := range arr2 {
        fmt.Printf("%T:%v\t", v, v)
    }
    fmt.Println("\n")

    fmt.Println("# array of map")
    arr3 := js.Get("test").Get("arraywithsubs").GetIndex(1).MustMap()
    fmt.Printf("%v\t", arr3)
    fmt.Printf("%v", arr3["subkeytwo"])
    fmt.Println("\n")

    //
    fmt.Println("# array of json")
    personArr, err := js.Get("person").Array()
    fmt.Printf("Number of person: %d\n", len(personArr))

    // Loop
    fmt.Println("\t# Loop person")
    for i, _ := range personArr {
        person := js.Get("person").GetIndex(i)
        name := person.Get("name").MustString()
        age := person.Get("age").MustInt()
        email := person.Get("email").MustString()

        fmt.Printf("\tname=%s, age=%d, email=%s\n", name, age, email)

        // get Phone number
        fmt.Println("\t# Loop phone number")
        phoneNumArr, _ := person.Get("phoneNum").Array()
        for ii, vv := range phoneNumArr {
            fmt.Println("\t", ii, vv)
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

Output:

---------------------get some field-----------------
simplejson 

---------------------check existence----------------
not exist!


---------------------array--------------------------
# array of string
asdf    ghjk    zxcv    

# array of number and string
json.Number:1   string:2    json.Number:3   

# array of map
map[subkeytwo:2 subkeythree:3]  2

# array of json
Number of person: 2
    # Loop person
    name=piao, age=30, email=piaoyunsoft@163.com
    # Loop phone number
     0 13974999999
     1 13984999999
    name=aaaaa, age=20, email=aaaaaa@163.com
    # Loop phone number
     0 13974998888
     1 13984998888
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

Reference

  1. go-simplejson|解析json字符串测试
  2. Golang–Simplejson简单实例
  3. go-simplejson
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/746868
推荐阅读
相关标签
  

闽ICP备14008679号