go-simplejson | json 문자열 분석

1966 단어 golang
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
", name, age, email) // phoneNumArr, _ := person.Get("phoneNum").Array() for ii, vv := range phoneNumArr { fmt.Println(ii, vv) } } }
http://www.dllhook.com/post/183.html

좋은 웹페이지 즐겨찾기