double0201
double0201
2015-07-23 20:34

遍历Go中的json数组以提取值

  • json

I have the following in my json array (conf.json file).

{
  "Repos": [
      "a",
      "b",
      "c"
  ]
}

I am attempting to read this json and then iterate over it but get stuck. I am very new to go (and to programming) so I am having a hard time understanding what is happening here.

import (
    "encoding/json"
    "fmt"
    "os"
)

type Configuration struct {
    Repos []string
}

func read_config() {
    file, _ := os.Open("conf.json")
    decoder := json.NewDecoder(file)
    configuration := Configuration{}
    err := decoder.Decode(&configuration)
    if err != nil {
        fmt.Println("error:", err)
    }

    fmt.Println(configuration.Repos)
}

So far this is as far as I have been able to get. This will print out the values okay, [a, b, c].

What I would like to do is be able to iterate over the array and split out each value individually but have not had any luck at doing this. Am I taking the wrong approach to this? Is there a better way to do this?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐