为了账号安全,请及时绑定邮箱和手机立即绑定

类型 *NewsAnswer 在尝试对结构进行范围时不支持索引

类型 *NewsAnswer 在尝试对结构进行范围时不支持索引

Go
千巷猫影 2022-05-23 17:25:05
我定义了如下结构type NewsAnswer struct {    ReadLink              string           `json:"readLink"`    QueryContext          queryContextJson `json:"queryContext"`    TotalEstimatedMatches int              `json:"totalEstimatedMatches"`    Sort                  []sortJson       `json:"sort"`    Value                 []valueJson      `json:"value"`}type queryContextJson struct {    OriginalQuery string `json:"originalQuery"`    AdultIntent   bool   `json:"adultIntent"`}type sortJson struct {    Name       string `json:"name"`    ID         string `json:"id"`    IsSelected bool   `json:"isSelected"`    URL        string `json:"url"`}type valueJson struct {    Name          string         `json:"name"`    URL           string         `json:"url"`    Image         imageJson      `json:"image"`    Description   string         `json:"description"`    Provider      []providerJson `json:"provider"`    DatePublished string         `json:"datePublished"`}type imageJson struct {    Thumbnail thumbnailJson `json:"thumbnail"`}type thumbnailJson struct {    ContentUrl string `json:"thumbnail"`    Width      int    `json:"width"`    Height     int    `json:"height"`}type providerJson struct {    Type string `json:"_type"`    Name string `json:"name"`}我在 API 响应中得到 JSON 我将 JSON 转换为如下结构body, err := ioutil.ReadAll(resp.Body)if err != nil {    panic(err)}ans := new(NewsAnswer)err = json.Unmarshal(body, &ans)if err != nil {  fmt.Println(err)}现在我想循环遍历数据,value所以我尝试像下面那样做范围for index, value := range ans["value"] {}但是遇到这个错误type *NewsAnswer does not support indexing 我该怎么办?
查看完整描述

1 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

您应该直接覆盖Value字段。

for index, value := range ans.Value {}


查看完整回答
反对 回复 2022-05-23
  • 1 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号