json数据解码的两种方法NewDecoder与Unmarshal

一、golang中处理http响应数据解码,一般有两种方式:

1:json.Unmarshal进行解码

func HandleUse(w http.ResponseWriter, r *http.Request) {    var u Use //此处的Use是一个结构体    data, err := ioutil.ReadAll(r.Body)//此处的r是http请求得到的json格式数据-->然后转化为[]byte格式数据.    if err != nil {        w.WriteHeader(http.StatusBadRequest)        return    }    if err := json.Unmarshal(data, &u); err != nil { //经过这一步将json解码赋值给结构体,由json转化为结构体数据        w.WriteHeader(http.StatusInternalServerError)        return    }    w.WriteHeader(http.StatusOK)    fmt.Fprintf(w, "姓名:%s,年龄:%d", u.Name, u.Age)}

2. json.NewDecoder解码

func HandleUse(w http.ResponseWriter, r *http.Request) {    var u Use    if err := json.NewDecoder(r.Body).Decode(&u); err != nil {        w.WriteHeader(http.StatusInternalServerError)        return    }    w.WriteHeader(http.StatusOK)    fmt.Fprintf(w, "姓名:%s,年龄:%d", u.Name, u.Age)}

二、区别:

1、json.NewDecoder是从一个里面直接进行解码,代码精干;
2、json.Unmarshal是从已存在与内存中的json进行解码;
3、相对于解码,json.NewEncoder进行大JSON的编码比json.marshal性能高,因为内部使用pool。

三、场景应用:

1、json.NewDecoder用于http连接与socket连接的读取与写入,或者文件读取;
2、json.Unmarshal用于直接是byte的输入。

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章