go-simplejson|解析json字符串

package main

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

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)

              }
       }

}
http://www.dllhook.com/post/183.html

你可能感兴趣的:(golang)