【发布时间】:2022-01-12 18:10:17
【问题描述】:
请帮助我解决以下问题,无论 json 响应的类型如何,条件都会被执行。这是一个返回 json 的示例公共 url,如果响应符合结构,我们只需记录标题。然而,无论什么 json 响应即将到来,代码都在条件内(err ==nil)。如果我没记错的话,json 解码器应该检查响应的结构。
我的代码在下面完成
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type response struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
func main() {
resp, err := http.Get("https://jsonplaceholder.typicode.com/posts/1")
if err != nil {
log.Fatalln(err)
}
var actual response
if err = json.NewDecoder(resp.Body).Decode(&actual); err == nil {
fmt.Printf("anotherLink from docker container: %s", actual.Title)
}
【问题讨论】:
-
默认 json.Decoder 不关心结构是否不同,只关心匹配目标结构的字段是否可以解码。如果您希望它在遇到不在
reponse中的字段时失败,请使用DisallowUnknownFields。但是请注意, json.Decoder 不提供“fail-if-field-is-missing”配置。因此,如果您在 json 中收到{},那么 json.Decoder 不会失败,而是您必须自己验证生成的response。