D
D
D55RUS2021-09-26 15:23:30
JSON
D55RUS, 2021-09-26 15:23:30

How to read json file in Go?

I have json file:

{
    "signatures": [
        "SIG_K1878787",
        "SIG_K14545454"
    ],
    "serializedTransaction": [
        153,
        97,
        80,
        97,
        183,
        14,
        213,
        148,
        201,
        215,
        0,
        0,
        0,
        0
    ]
}

I need to read it and extract the signatures

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
veryoriginalnickname, 2021-09-26
@veryoriginalnickname

for example here:

import (
  "encoding/json"
  "io/ioutil"
)

func GetSignatures() []string {
  // тут можно удобно перегонять json в структуры https://mholt.github.io/json-to-go/
  type JsonGo struct {
    Signatures            []string `json:"signatures"`
    SerializedTransaction []int    `json:"serializedTransaction"`
  }
  var st JsonGo
  path := "какой-то-путь/file.json"
  byteValue, err := ioutil.ReadFile(path)
  if err != nil {
    panic(err)
  }
  err = json.Unmarshal(byteValue, &st)
  if err != nil {
    panic(err)
  }
  return st.Signatures
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question