V
V
Vitaly B2019-08-12 08:11:21
go
Vitaly B, 2019-08-12 08:11:21

How to parse json file in Go?

I have a json file

[{"server":"28773abe-e620-4d36-9512-c6f4b128f0ad"},{"server":"11c044ac-00eb-4fc6-9c30-fccab0ba8cda"}...]

How to parse a file and get each value by the server key so that you can then pass the value to another function?
type Servers struct {
  Server string `json:"server"`
}
const slist = "global_servers.json"
func Firstfunc() {
  f, err := os.Open(slist)
  if nil != err {
    log.Fatalln(err)
  }
  defer f.Close()
  dec := json.NewDecoder(f)

  db := []Servers{}
  dec.Decode(&db)
  fmt.Println(db)
  for field, value := range db {
    fmt.Print(field, "\n")
    fmt.Print(value, "\n")
                 // в value получаю {28773abe-e620-4d36-9512-c6f4b128f0ad}
                // нужна строка 28773abe-e620-4d36-9512-c6f4b128f0ad
    // и вот здесь хотелось бы Secondfunc(value), но ошибка
  }
}
func Secondfunc(srt string) {

  fmt.Println(srt)
}

Cannot use value (type Servers) as type string in argument to Secondfunc error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2019-08-12
@vitaliy_balahnin

func Secondfunc(srt Servers)
or
Secondfunc(value.Server)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question