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条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站