Answer the question
In order to leave comments, you need to log in
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)
}
}
{"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}}
"poolHash":81535192
"poolTime":"2017-01-08 17:52"
"now":1483887170529
"poolCharts":[{"now":1483887170529,"poolTime":"2017-01-08 17:52","poolHash":81535192}]
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question