dongzou3751 2018-07-22 20:47
浏览 59
已采纳

迭代JSON对象数组

I just started learning Go and I'm trying to iterate through each element of array of JSON objects.

I tried the following.

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])
    }

}

How can I iterate on each JSON object?

Expected Output:

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

2条回答 默认 最新

  • dongpo7467 2018-07-22 21:01
    关注
    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

    Edit:

    Your intent to iterate structural data as sequential strings looks very unusual. Sure you can, just construct Decoder and 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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效