go JSON

package utils

import (
    "encoding/json"
    "errors"
)

func JsonToMap(text []byte) (map[string]interface{}, error) {

    var anonymous interface{}
    err := json.Unmarshal(text, &anonymous)
    if err != nil {
        return nil, errors.New(err.Error())
    }   
    res := anonymous.(map[string]interface{})

    return res, nil                                                                                                                                                     
}

参考:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/07.2.md

posted @ 2016-08-24 14:47  hao.ma  阅读(223)  评论(0编辑  收藏  举报