- Unmarshal2Map

This commit is contained in:
gazebo
2018-11-19 20:10:22 +08:00
parent d452cdb5c6
commit 5a43ff8e20
2 changed files with 58 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ import (
"time"
"git.rosy.net.cn/baseapi"
"github.com/fatih/structs"
)
var (
@@ -40,6 +41,23 @@ func UnmarshalUseNumber(data []byte, result interface{}) error {
return err
}
// 这个函数将解析json返回的map中的字段类型与structObj中的完全一样的
func Unmarshal2Map(data []byte, structObj interface{}) (resultMap map[string]interface{}, err error) {
if err = json.Unmarshal(data, structObj); err == nil {
if err = json.Unmarshal(data, &resultMap); err == nil {
m := structs.Map(structObj)
for k := range resultMap {
if value, ok := m[k]; ok {
resultMap[k] = value
} else {
delete(resultMap, k)
}
}
}
}
return resultMap, err
}
func MustMarshal(obj interface{}) []byte {
byteArr, err := json.Marshal(obj)
if err != nil {