discovered that go json decoding is a big pain in the butt so please help. Here's my json:
{ "BTC_BCN":{ "id":7, "last":"0.00000156", "lowestAsk":"0.00000156", "highestBid":"0.00000155", "percentChange":"0.01960784", "baseVolume":"4920.84786257", "quoteVolume":"3016048494.19305944", "isFrozen":"0", "high24hr":"0.00000183", "low24hr":"0.00000145" }, "BTC_BELA":{ "id":8, "last":"0.00008847", "lowestAsk":"0.00008848", "highestBid":"0.00008847", "percentChange":"-0.00405268", "baseVolume":"169.66498061", "quoteVolume":"1981232.44495809", "isFrozen":"0", "high24hr":"0.00008995", "low24hr":"0.00008120" }, ... } So I need to put that in a type that I created
//Crypto is the currency object type Crypto struct { iso string //this is the key (ex: BTC_BCN) id int last float64 lowestAsk float64 highestBid float64 percentChange float64 baseVolume float64 quoteVolume float64 isFrozen int high24hr float64 low24hr float64 } and here is what I did so far, but I ended up with the keys in place and an empty value
func main() { // sendEmail("Some text") currencies := getCurrencies() if currencies == nil { return } fmt.Println(len(currencies)) } func getCurrencies() map[string]Crypto { curList := make(map[string]Crypto) resp, err := http.Get("https://poloniex.com/public?command=returnTicker") // fmt.Println(err) if err != nil { sendEmail("Error getting data from poloniex " + err.Error()) return nil } body, readErr := ioutil.ReadAll(resp.Body) reader := strings.NewReader(string(body)) jsonErr := json.NewDecoder(reader).Decode(&curList) // fmt.Printf("curList is : %#v\n", curList) // fmt.Printf("body is : %s\n", string(body)) if readErr != nil { fmt.Printf("readErr: %s\n", readErr.Error()) } if jsonErr != nil { fmt.Printf("jsonErr: %s\n", jsonErr.Error()) } for k, v := range curList { fmt.Println("k:", k, "v:", v) } defer resp.Body.Close() return curList } output:
k: BTC_MAID v: {0 0 0 0 0 0 0 0 0 0} k: BTC_NOTE v: {0 0 0 0 0 0 0 0 0 0} k: BTC_VRC v: {0 0 0 0 0 0 0 0 0 0} k: BTC_DOGE v: {0 0 0 0 0 0 0 0 0 0}... Please excuse my stupid question but I've spent days on it and I think I am missing something. Cheers.