从 http 请求中解组嵌套 json 对象返回 nil

我在这里遇到过其他类似的问题,但我不知道我做错了什么。我正在调用这个API:


 https://coronavirus-tracker-api.herokuapp.com/v2/locations

它返回一个像这样的 JSON 对象:


{

  "latest": {

     "confirmed": 272166,

     "deaths": 11299,

     "recovered": 87256

   },

   "locations": [

    {

       "id": 0,

       "country": "Thailand",

       "country_code": "TH",

      "province": "",

      "last_updated": "2020-03-21T06:59:11.315422Z",

      "coordinates": {

        "latitude": "15",

        "longitude": "101"

     },

       "latest": {

       "confirmed": 177,

        "deaths": 1,

       "recovered": 41

      }

   },

    {

      "id": 39,

     "country": "Norway",

     "country_code": "NO",

      "province": "",

      "last_updated": "2020-03-21T06:59:11.315422Z",

      "coordinates": {

        "latitude": "60.472",

        "longitude": "8.4689"

     },

      "latest": {

        "confirmed": 1463,

        "deaths": 3,

        "recovered": 1

      }

    }

  ]

}

所以我编写了一个小程序来解析它,但我只能解析外部对象(“最新”),而内部数组(“位置”)始终返回nil。


代码在这里(即使 TCP 调用在 Playground 上不起作用):


 https://play.golang.org/p/ma225d07iRA


蝴蝶刀刀
浏览 74回答 1
1回答

明月笑刀无情

问题是端点https://coronavirus-tracker-api.herokuapp.com/v2/latest没有返回locations。这是我通过调用它得到的响应:{    "latest": {        "confirmed": 304524,        "deaths": 12973,        "recovered": 91499    }}但是,如果您调用正确的端点https://coronavirus-tracker-api.herokuapp.com/v2/locations,它可能会起作用。
打开App,查看更多内容
随时随地看视频慕课网APP