SimpleJson 用法示例

无须事先定义结构体信息,非常灵活

package main

import (
    "encoding/json"
    "fmt"
    "github.com/bitly/go-simplejson" // for json get
)

var jsonStr = `
       {
           "person": [{
              "name": "piao",
              "age": 30,
              "email": "[email protected]",
              "phoneNum": [
                  "13974999999",
                  "13984999999"
              ]
           }, {
              "name": "aaaaa",
              "age": 20,
              "email": "[email protected]",
              "phoneNum": [
                  "13974998888",
                  "13984998888"
              ]
           }, {
              "name": "bbbbbb",
              "age": 10,
              "email": "[email protected]",
              "phoneNum": [
                  "13974997777",
                  "13984997777"
              ]
           }]
       }
       `

func main() {
    js, err := simplejson.NewJson([]byte(jsonStr))

    if err != nil {
        panic(err.Error())
    }

    personArr, err := js.Get("person").Array()
    fmt.Println(len(personArr))

    // 遍历
    for i, _ := range personArr {
        //fmt.Println(i, v)
        person := js.Get("person").GetIndex(i)
        name := person.Get("name").MustString()
        age := person.Get("age").MustInt()
        email := person.Get("email").MustString()

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

        // 读取手机号
        phoneNumArr, _ := person.Get("phoneNum").Array()
        for ii, vv := range phoneNumArr {
            fmt.Println(ii, vv)
        }
    }

    // make a json string 0
    if true {
        jData := make(map[string]interface{})

        var persionArray []interface{}
        for i := 0; i < 2; i++ {
            persion := make(map[string]interface{})

            if true {
                persion["name"] = "piao"
                persion["age"] = 30
                persion["email"] = "piao1@email"

                if true {
                    var phoneNumArray []interface{}

                    phoneNumArray = append(phoneNumArray, "13793135326")
                    phoneNumArray = append(phoneNumArray, "13793135327")

                    persion["phoneNum"] = phoneNumArray
                }

            }

            persionArray = append(persionArray, persion)
        }

        jData["persion"] = persionArray
        c, _ := json.Marshal(jData)
        fmt.Println(string(c))
    }

    // make a json string 1
    if true {
        a := []byte(`{"Parents": [ "aaaaa", "bbbbbbb" ]}`)
        b := []byte(`{"Parents": [ "Gomez", "Moticia" ]}`)
        var arr []interface{}

        js, _ := simplejson.NewJson(a)
        nodes, _ := js.Map()
        p := nodes["Parents"]
        d := p.([]interface{})
        for _, v := range d {
            arr = append(arr, v)
        }

        js, _ = simplejson.NewJson(b)
        nodes, _ = js.Map()
        p = nodes["Parents"]
        d = p.([]interface{})
        for _, v := range d {
            arr = append(arr, v)
        }

        res := make(map[string]interface{})
        res["Parents"] = arr

        c, _ := json.Marshal(res)
        fmt.Println(string(c))
    }
}

你可能感兴趣的:(SimpleJson 用法示例)