这是我第一次尝试 Go,我觉得我在这里遗漏了一些重要的东西。试图解码来自网络服务的 JSON 消息,但我得到的输出是:{响应:{请求:[]}}我真正感兴趣的是请求节点中的数据。我的 for 循环显然没有被调用,因为数组是空的。我觉得我的结构需要完全按照它们出现在 web 服务中的方式进行声明?示例 JSON:{"response": {"requests": [{"request": {}},{"request": { "id": 589748, "image_thumbnail": "", "description": "Blah blah", "status": "received", "user": "test" }}],"count": "50","benchmark": 0.95516896247864,"status": {},"debug": {}}}type Request struct { id int `json:"id"` description string `json:"description"` user string `json:"user"`}type Requests struct { request Request `json:"request"`}type Response struct { requests []Requests `json:"requests"` } type RootObject struct { response Response `json:"response"` }url := "<webservice>"req, err := http.NewRequest("GET", url, nil)req.Header.Set("Content-Type", "application/json")client := &http.Client{}resp, err := client.Do(req)if err != nil { panic(err)}defer resp.Body.Close()var r RootObjectdecoder := json.NewDecoder(resp.Body)decoder.Decode(&r)fmt.Printf("%+v", r)for _, req := range r.response.requests { fmt.Printf("%d = %s\n", req.request.id, req.request.user)}
- 1 回答
- 0 关注
- 155 浏览
添加回答
举报
0/150
提交
取消