从压缩的 HTTP 中解组 JSON:寻找值开头的无效字符

Go unmarshalling JSON from compessed HTTP: invalid character looking for beginning of value

我刚刚编写了我的第一个 Go 应用程序,它通过 http 下载和解组简单的 JSON 对象。 Http 内容被压缩: 'content-encoding': 'deflate'

我用了几个著名的例子(比如this)。不幸的是,应用程序无法解析所需的 JSON,并出现非常罕见且奇怪的错误。我无法找出问题所在。任何帮助将不胜感激。

JSON 输入 (Python 用于调试)

In [8]: r = requests.get("http://172.17.0.31:20000/top")

In [9]: r.text
Out[9]: u'{"timestamp":{"tv_sec":1428447555,"tv_usec":600186},"string_timestamp":"2015-04-07 22:59:15.600186","monitor_status":"enabled"}'
In [18]: r.headers
Out[18]: {'content-length': '111', 'content-type': 'application/json', 'connection': 'close', 'content-encoding': 'deflate'}

源代码已更新根据答案)

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type Top struct {
    Timestamp        Timestamp `json:"timestamp"`
    String_timestamp string    `json:"string_timestamp"`
    Monitor_status   string    `json:"monitor_status"`
}

type Timestamp struct {
    Tv_sec  int `json:"tv_sec"`
    Tv_usec int `json:"tv_usec"`
}

func get_content() {

    url := "http://172.17.0.31:20000/top"

    res, err := http.Get(url)
    if err != nil {
        panic(err.Error())
    }
    fmt.Println(res)

    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        panic(err.Error())
    }
    fmt.Println(body)

    var jsondata Top
    err = json.Unmarshal(body, &jsondata)
    if err != nil {
        panic(err.Error())
    }

    fmt.Println(jsondata)
}

func main() {
    get_content()
}

错误

[vitaly@thermaltake elliptics-manager]$ go run main.go 
&{200 OK 200 HTTP/1.1 1 1 map[Content-Type:[application/json] Content-Length:[111] Content-Encoding:[deflate]] 0xc20803e340 111 [] true map[] 0xc208028820 <nil>}
[120 156 77 203 65 14 130 48 16 70 225 171 152 127 93 76 59 51 162 244 50 13 96 99 154 216 98 232 192 134 112 119 81 55 110 95 190 183 65 83 142 85 251 252 130 223 160 107 168 113 132 119 66 55 145 182 117 108 62 109 249 70 98 234 108 183 27 84 157 83 121 132 191 19 100 221 165 177 210 216 235 137 200 11 123 230 243 207 195 32 79 37 233 52 135 3 235 82 15 29 75 63 60 227 29 251 27 195 90 38 189]
panic: invalid character 'x' looking for beginning of value

更新: 谢谢大家。现在很明显,这个问题的原因是 HTTP 响应的 deflate 压缩。但是,目前还不清楚如何在 Golang 中执行解压(参见)。

我不知道 'x',但结构字段必须是 public(以大写字母开头)才能被 json Unmarshaller 考虑。当然,名称与 json 键不匹配,您必须添加 json 注释,如下所示:

type Top struct {
    Timestamp        Timestamp `json:"timestamp"`
    String_timestamp string `json:"string_timestamp"`
    Monitor_status   string `json:"monitor_status"`
}

我认为这是由于您进行了双重编码。 ioutil.ReadAll(res.Body) returns a []byte 所以当你做 []byte(body) 你正在转换已经是字节数组的东西,我猜第一个字节的 UTF 值是 x .只需更新此; json.Unmarshal([]byte(body), &jsondata)json.Unmarshal(body, &jsondata) 我打赌它会很好地解组。

此外,与您的错误无关,但正如另一个答案中指出的那样,如果您不导出结构中的字段(在 go 中,这意味着字段名称以大写字母开头),那么解组器将无法使他们的用户。要完成这项工作,您需要将您的类型更新为;

type Top struct {
    Timestamp        Timestamp `json:"timestamp"`
    String_timestamp string `json:"string_timestamp"`
    Monitor_status   string `json:"monitor_status"`
}

json 注释是必需的,因为解组器非常严格并且要求字段名称完全匹配(区分大小写)。

Go JSON 编组器只能编组 unicode 字符串。看来你的 JSON 不是用 unicode 编码的,而是用其他编码(放气?)。

如果您采用字节流:

[120 156 77 203 65 14 130 48 16 70 225 171 152 127 93 76 59 51 162 244 50 13 96 99 154 216 98 232 192 134 112 119 81 55 110 95 190 183 65 83 142 85 251 252 130 223 160 107 168 113 132 119 66 55 145 182 117 108 62 109 249 70 98 234 108 183 27 84 157 83 121 132 191 19 100 221 165 177 210 216 235 137 200 11 123 230 243 207 195 32 79 37 233 52 135 3 235 82 15 29 75 63 60 227 29 251 27 195 90 38 189]

并尝试从中获取一个 unicode 字符串:

body := []byte{120, 156, 77, 203, 65, 14, 130, 48, 16, 70, 225, 171, 152, 127, 93, 76, 59, 51, 162, 244, 50, 13, 96, 99, 154, 216, 98, 232, 192, 134, 112, 119, 81, 55, 110, 95, 190, 183, 65, 83, 142, 85, 251, 252, 130, 223, 160, 107, 168, 113, 132, 119, 66, 55, 145, 182, 117, 108, 62, 109, 249, 70, 98, 234, 108, 183, 27, 84, 157, 83, 121, 132, 191, 19, 100, 221, 165, 177, 210, 216, 235, 137, 200, 11, 123, 230, 243, 207, 195, 32, 79, 37, 233, 52, 135, 3, 235, 82, 15, 29, 75, 63, 60, 227, 29, 251, 27, 195, 90, 38, 189}
fmt.Println(string(body))

您会在控制台中看到一个奇怪的(压缩的?)字符串,而不是 JSON。

我猜想 python http 客户端会自动解压缩压缩后的字节,而 Go http 客户端不会(我知道它对 gzip 是这样做的,但不确定是否对压缩压缩)。在您能够使用 JSON 编组器解析它们之前,您必须读出压缩的字节并将它们转换为 unicode 字符串。

请试试这个

func get_content() {

    url := "http://172.17.0.31:20000/top"

    res, err := http.Get(url)
    if err != nil {
        panic(err.Error())
    }
    defer res.Body.Close()

    fmt.Println("res body:", res.Body)

    body, err := ioutil.ReadAll(resp=.Body)

    fmt.Println("body:", body)
    re, err := zlib.NewReader(bytes.NewReader(body))
    fmt.Println("zlib:", re)
    enflated, err := ioutil.ReadAll(re)
    fmt.Println("enflated:", string(enflated))

    var jsondata Top
    err = json.Unmarshal(body, &jsondata)
    if err != nil {
        panic(err.Error())
    }

    fmt.Println(jsondata)
}

并确保 http://172.17.0.31:20000/top return json 类型。