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

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择