package main import ( "encoding/json" "fmt" "github.com/iancoleman/orderedmap" ) func main() { o := orderedmap.New() o.Set("error_code", 1) o.Set("error_msg", "fagaga") s := orderedmap.New() s.Set("total",10) aggs := orderedmap.New() aggs.Set("1001€品牌",[...]string{"3001€mouser€0","4003€arrowV€0"}) aggs.Set("1002€标称电压",[...]string{"2001€5.5V€0","4003€arrowV€0"}) s.Set("aggs",aggs) o.Set("data", s) m1,_ := o.Get("error_msg") fmt.Print(m1) bytes, err := json.Marshal(o) fmt.Print(string(bytes),err) return //// use Get instead of i, ok := o["a"] //val, ok := o.Get("a") // //// use Keys instead of for k, v := range o //key := o.Keys() //for _, k := range keys { // v, _ := o.Get(k) //} // use o.Delete instead of delete(o, key) //err := o.Delete("a") // serialize to a json string using encoding/json //bytes, err := json.Marshal(o) //fmt.Print(string(bytes),err) //prettyBytes, err := json.MarshalIndent(o) // //// deserialize a json string using encoding/json //// all maps (including nested maps) will be parsed as orderedmaps //s := `{"a": 1}` //err := json.Unmarshal([]byte(s), &o) // //// sort the keys //o.SortKeys(sort.Strings) // //// sort by Pair //o.Sort(func(a *Pair, b *Pair) bool { // return a.Value().(float64) < b.Value().(float64) //}) }