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

我定义了如下结构


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 我该怎么办?


千巷猫影
浏览 118回答 1
1回答

狐的传说

您应该直接覆盖Value字段。for index, value := range ans.Value {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go