【发布时间】:2017-06-24 20:23:58
【问题描述】:
响应结构如下:
type Response struct {
Message string `json:"message"`
}
代码如下:
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
response := &Response{}
json.NewDecoder(resp.Body).Decode(response)
fmt.Println("response struct:", response)
输出如下:
response Body: {"Message":"success"}
response struct: &{}
正如我们所见,响应正文字符串很好,并且包含 json 字符串。但是当我尝试将响应正文解码为 json 时,我得到一个空结构。
我已经在结构中导出了 Message 字段,以便 json 包可以访问它。我在这里还缺少什么?
【问题讨论】:
-
重复但懒得查。
-
重复的就是没有导出字段的地方。
-
不,使用正文进行调试打印并想知道为什么再次解码它会产生空也很常见。
标签: go