迭代JSON对象数组

时间:2018-07-22 20:47:04

标签: arrays json loops go iteration

我刚刚开始学习Go,并且尝试遍历JSON对象数组的每个元素。

我尝试了以下方法。

package main

import (
    "encoding/json"
    "fmt"
)

type itemdata []string

func main() {
    var birds itemdata
    birdJson := `[{"species":"pigeon","decription":"likes to perch on rocks"},{"species":"eagle","description":"bird of prey"},{"species":"eagle","description":"bird of prey"}]`

    json.Unmarshal([]byte(birdJson), &birds)
    fmt.Println(len(birds))
    fmt.Println(birds)
    for i := range birds {
        fmt.Println(i)
        fmt.Println(birds[i])
    }

}

如何迭代每个JSON对象?

预期输出:

0
{"species":"pigeon","decription":"likes to perch on rocks"}
1
{"species":"eagle","description":"bird of prey"}
2
{"species":"eagle","description":"bird of prey"}

2 个答案:

答案 0 :(得分:1)

package main

import (
    "encoding/json"
    "fmt"
)

type itemdata []struct {    //Precise definition of data structure
    Species     string
    Description string
}

func main() {
    var birds itemdata
    birdJson := `[{"species":"pigeon","description":"likes to perch on rocks"},{"species":"eagle","description":"bird of prey"},{"species":"eagle","description":"bird of prey"}]`

    json.Unmarshal([]byte(birdJson), &birds)
    fmt.Println(len(birds))
    fmt.Println(birds)
    for i, bird := range birds {   //correct syntax for loop
        fmt.Println(i)
        fmt.Println(bird)
    }

}

Playground

编辑:

您要遍历顺序字符串的结构数据看起来很不寻常。当然可以,只需构造Decoder和tokeniser。

type itemdata []json.RawMessage //delay unmarshaling of array items
func main() {
    var birds itemdata
    birdJson := `[{"species":"pigeon","description":"likes to perch on rocks"},{"species":"eagle","description":"bird of prey"},{"species":"eagle","description":"bird of prey"}]`
    json.Unmarshal([]byte(birdJson), &birds)
    fmt.Println(len(birds))
    fmt.Println(birds)
    for i, bird := range birds {
        fmt.Println(i)
        dec := json.NewDecoder(bytes.NewReader(bird)) //construct Decoder
        for {
            t, err := dec.Token()  //Tokenise
            if err == io.EOF {
                break
            }
            if _, ok := t.(json.Delim); !ok {
                fmt.Println(t)
            }
        }
    }
}

Playground

答案 1 :(得分:1)

package main

import (
    "encoding/json"
    "fmt"
)

type bird struct{
    Species string
    Description string
}

func main() {
    var birds []bird
    birdJson := `[{"species":"pigeon","description":"likes to perch on rocks"},
                  {"species":"eagle","description":"bird of prey"},
                  {"species":"falcon","description":"yet another bird of prey"}]`

    json.Unmarshal([]byte(birdJson), &birds)
    fmt.Println(len(birds))
    fmt.Printf("\n%+v\n\n", birds)
    for i := range birds {
        fmt.Printf("%d) %s: %s\n", i, birds[i].Species, birds[i].Description)
    }
}

使用正确的循环语法:

for i,bird := range birds {
    fmt.Printf("%d) %s: %s\n", i, bird.Species, bird.Description)
}

编辑:这是您的意思吗?

package main

import (
    "encoding/json"
    "fmt"
)

type bird struct{
    Species string `json:"species"`
    Description string `json:"description"`
}

func main() {
    var birds []bird
    birdJson := `[{"species":"pigeon","description":"likes to perch on rocks"},
                  {"species":"eagle","description":"bird of prey"},
                  {"species":"falcon","description":"yet another bird of prey"}]`

    json.Unmarshal([]byte(birdJson), &birds)
    fmt.Println(len(birds))
    fmt.Printf("\n%+v\n\n", birds)
    for _,bird := range birds {
        //fmt.Printf("%d) %s: %s\n", i, bird.Species, bird.Description)
        b, _ := json.Marshal(bird)
        fmt.Println(string(b))
    }
}
相关问题