dp152153 2018-09-28 12:51
浏览 16

在Go中解组复杂的json

So I am trying to fetch the analytics of an app by pinging and endpoint. I make the GET request which is successfull (no errors there) but I am unable to decode the JSON

I need to to decode the following json into structs

{
  "noResultSearches": {
    "results": [
      {
        "count": 1,
        "key": "\"note 9\""
      },
      {
        "count": 1,
        "key": "nokia"
      }
    ]
  },
  "popularSearches": {
    "results": [
      {
        "count": 4,
        "key": "6"
      },
      {
        "count": 2,
        "key": "\"note 9\""
      },
      {
        "count": 1,
        "key": "nokia"
      }
    ]
  },
  "searchVolume": {
    "results": [
      {
        "count": 7,
        "key": 1537401600000,
        "key_as_string": "2018/09/20 00:00:00"
      }
    ]
  }
}

For which I am using the following structs

type analyticsResults struct {
    Count int    `json:"count"`
    Key   string `json:"key"`
}

type analyticsVolumeResults struct {
    Count     int    `json:"count"`
    Key       int64 `json:"key"`
    DateAsStr string `json:"key_as_string"`
}

type analyticsPopularSearches struct {
    Results []analyticsResults `json:"results"`
}

type analyticsNoResultSearches struct {
    Results []analyticsResults `json:"results"`
}

type analyticsSearchVolume struct {
    Results []analyticsVolumeResults `json:"results"`
}

type overviewAnalyticsBody struct {
    NoResultSearches analyticsNoResultSearches `json:"noResultSearches"`
    PopularSearches  analyticsPopularSearches  `json:"popularSearches"`
    SearchVolume     analyticsSearchVolume     `json:"searchVolume"`
}

I make a GET request to an endpoint and then use the response body to decode the json but I get an error. Following is a part of the code that stays in my ShowAnalytics function

func ShowAppAnalytics(app string) error {
  spinner.StartText("Fetching app analytics")
  defer spinner.Stop()

  fmt.Println()
  req, err := http.NewRequest("GET", "<some-endpoint>", nil)
  if err != nil {
      return err
  }
  resp, err := session.SendRequest(req)
  if err != nil {
      return err
  }
  spinner.Stop()

  var res overviewAnalyticsBody
  dec := json.NewDecoder(resp.Body)
  err = dec.Decode(&res)
  if err != nil {
      return err
  }

  fmt.Println(res)

  return nil
}

json: cannot unmarshal array into Go struct field overviewAnalyticsBody.noResultSearches of type app.analyticsNoResultSearches

What am I doing wrong here? Why do I get this error?

  • 写回答

1条回答 默认 最新

  • doubu1970 2018-09-28 12:58
    关注

    EDIT: After you edited, your current code works as-is. Check it out here: Go Playground.

    Original answer follows.


    There is some inconsistency between the code you posted and the error you get.

    I tried it on the Go Playground (here's your version), and I get the following error:

    json: cannot unmarshal number into Go struct field analyticsVolumeResults.key of type string

    We get this error because in the JSON searchVolume.results.key is a number:

        "key": 1537401600000,
    

    And you used string in the Go model:

    Key       string `json:"key"`
    

    If we change it to int64:

    Key       int64 `json:"key"`
    

    It works, and prints (try it on the Go Playground):

    {{[{1 "note 9"} {1 nokia}]} {[{4 6} {2 "note 9"} {1 nokia}]} {[{7 1537401600000 2018/09/20 00:00:00}]}}

    If that key may sometimes be a number and sometimes a string, you may also use json.Number in the Go model:

    Key       json.Number `json:"key"`
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助