B
B
Belmix2017-01-08 18:19:34
API
Belmix, 2017-01-08 18:19:34

How to pass the following format to json?

Help with GO

Func (s *ApiServer) StatsIndex(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  w.Header().Set("Access-Control-Allow-Origin", "*")
  w.Header().Set("Cache-Control", "no-cache")
  w.WriteHeader(http.StatusOK)

  reply := make(map[string]interface{})
  nodes, err := s.backend.GetNodeStates()
  if err != nil {
    log.Printf("Failed to get nodes stats from backend: %v", err)
  }
  reply["nodes"] = nodes

  stats := s.getStats()
  v1 := stats["hashrate"]
  v2 := time.Now()
  if stats != nil {
    reply["now"] = util.MakeTimestamp()
    reply["poolHash"] = v1
    reply["poolTime"] = v2.Format("2006-01-02 15:04")
    reply["stats"] = stats["stats"]
    reply["hashrate"] = stats["hashrate"]
    reply["minersTotal"] = stats["minersTotal"]
    reply["maturedTotal"] = stats["maturedTotal"]
    reply["immatureTotal"] = stats["immatureTotal"]
    reply["candidatesTotal"] = stats["candidatesTotal"]
  
                
  }

  err = json.NewEncoder(w).Encode(reply)
  if err != nil {
    log.Println("Error serializing API response: ", err)
  }
}

there is a function, through it data is displayed in json
{"candidatesTotal":0,"hashrate":81535192,"immatureTotal":0,"maturedTotal":18,"minersTotal":2,
"nodes":[{"difficulty":"13420520413203","height":"2968066","lastBeat":"1483887170","name":"main"}],
"now":1483887170529,"poolHash":81535192,"poolTime":"2017-01-08 17:52",
"stats":{"lastBlockFound":1483841220,"nShares":1000,"roundShares":3980000000000}}

they have data
"poolHash":81535192
"poolTime":"2017-01-08 17:52"
"now":1483887170529

how to make them look like this?
"poolCharts":[{"now":1483887170529,"poolTime":"2017-01-08 17:52","poolHash":81535192}]

it looks like an array of one structure, but I could be wrong, I don’t know more about
go
v1 := stats["hashrate"] //тип interface{}
v2 := time.Now() //тип time.Time
reply["now"] = util.MakeTimestamp() //тоже возвращает тип time.Time

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2017-01-09
@mututunus

reply["poolCharts"] = []interface{}{
  map[string]interface{}{
    "poolHash": v1,
    "poolTime": v2.Format("2006-01-02 15:04"),
    "now": util.MakeTimestamp(),
  },
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question