go - json decode key types -
i want decode big set of data (static-schema) json file. file contains exclusively numeric data, , keys integers. know how decode json struct containing fields of map[string]int or map[string]float32 using json.unmarshal. have no interest in string keys, i'd need convert them int somehow.
so i'd know is:
- is there way achieve this, .ie getting struct containing fields of map[int]float32 type directly decoding,
- otherwise how achieve after decoding, in memory efficient manner ?
thanks
the json format specifies key/value objects string keys. because of this, encoding/json package supports string keys well.
the json/encoding documentation states:
bool, json booleans
float64, json numbers
string, json strings
[]interface{}, json arrays
map[string]interface{}, json objects
nil json null
if want use encoding/json package , move on map[int]float64, can following (works float32 well):
package main  import (     "fmt"     "strconv" )  func main() {     := map[string]float64{"1":1, "2":4, "3":9, "5":25}     b := make(map[int]float64, len(a))      k,v := range {         if i, err := strconv.atoi(k); err == nil {             b[i] = v         } else {             // non integer key         }     }      fmt.printf("%#v\n", b) } 
Comments
Post a Comment