|
- package comm
-
- import (
- "bytes"
- "encoding/json"
- "sort"
- "strings"
- )
-
- func Ksort(params map[string]interface{}) []string {
- keys := make([]string, len(params))
- i := 0
- for k, _ := range params {
- keys[i] = k
- i++
- }
- sort.Strings(keys)
- return keys
- }
- func KsortToStr(params map[string]string) []string {
- keys := make([]string, len(params))
- i := 0
- for k, _ := range params {
- keys[i] = k
- i++
- }
- sort.Strings(keys)
- return keys
- }
-
- // Marshal 序列化参数
- func GetSortJson(o interface{}) string {
- // 序列化一次
- raw, _ := json.Marshal(o)
-
- // 反序列化为map
- m := make(map[string]interface{})
- reader := bytes.NewReader(raw)
- decode := json.NewDecoder(reader)
- decode.UseNumber()
- _ = decode.Decode(&m)
-
- // 重新做一次序列化,并禁用Html Escape
- buffer := bytes.NewBufferString("")
- encoder := json.NewEncoder(buffer)
- encoder.SetEscapeHTML(false)
- _ = encoder.Encode(m)
-
- marshal := strings.TrimSpace(buffer.String()) // Trim掉末尾的换行符
- return marshal
- }
|