dongtang9855 2016-08-16 18:27
浏览 188
已采纳

用Go解码gZip json

As a Go newbie it's difficult for me to pinpoint the problem area, but hopefully giving you some facts will help.

I'm playing with an API which returns its Content-Encoding as gzip. I have written the following to encode my response struct:

reader, err = gzip.NewReader(resp.Body)
defer reader.Close()

// print to standard out
//_, err = io.Copy(os.Stdout, reader)
//if err != nil {
//  log.Fatal(err)
//}

// Decode the response into our tescoResponse struct
var response TescoResponse
err := json.NewDecoder(reader).Decode(&response)

I've removed the error handling for brevity, but the point of interest is that if I uncomment the print to stdout, I get the expected result. However, the decode doesn't give me what I expect. Any pointers? Is it that the struct has to map exactly to the response??

Here's the full example: https://play.golang.org/p/4eCuXxXm3T

  • 写回答

2条回答 默认 最新

  • drzk21632 2016-08-18 19:27
    关注

    From the documenation:

    DisableCompression, if true, prevents the Transport from requesting compression with an "Accept-Encoding: gzip" request header when the Request contains no existing Accept-Encoding value. If the Transport requests gzip on its own and gets a gzipped response, it's transparently decoded in the Response.Body. However, if the user explicitly requested gzip it is not automatically uncompressed.

    Proposed solution:

    type gzreadCloser struct {
        *gzip.Reader
        io.Closer
    }
    
    func (gz gzreadCloser) Close() error {
        return gz.Closer.Close()
    }
    

    // then in your http call ....

        if resp.Header.Get("Content-Encoding") == "gzip" {
            resp.Header.Del("Content-Length")
            zr, err := gzip.NewReader(resp.Body)
            if err != nil {
                return nil, err
            }
            resp.Body = gzreadCloser{zr, resp.Body}
        }
    
    // then you will be able to decode the json transparently
    
    if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
    
    }
    

    Adapted solution from your code: https://play.golang.org/p/Vt07y_xgak

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站