是否有用于 json 解组的 "any" 标记?

Is there an "any" tag for json unmarshal?

我想对coinmarketcap进行两次调用,两次调用的响应几乎相同,只有一个ID不同
如果我用 id="1" 调用,那么 repsonse 结构将是这样的

{
  "status": {
    "timestamp": "2022-01-31T11:08:10.552Z",
    "error_code": 0,
    "error_message": null,
    "elapsed": 29,
    "credit_count": 1,
    "notice": null
  },
  "data": {
    "1": {
      "id": 1,
      "name": "Bitcoin",
      "symbol": "BTC",
      "slug": "bitcoin",
      "num_market_pairs": 9121,
      "date_added": "2013-04-28T00:00:00.000Z",
      "max_supply": 21000000,
      [...]
      "last_updated": "2022-01-31T11:06:00.000Z",
      "quote": {
        "USD": {
          "price": 37287.959833145724,
          "volume_24h": 16426509863.271738,
          "volume_change_24h": -5.6098,
          "percent_change_1h": 0.18350099,
          "percent_change_24h": -2.27056162,
          "percent_change_7d": 11.98926671,
          "percent_change_30d": -20.53627257,
          "percent_change_60d": -33.95545743,
          "percent_change_90d": -40.97732382,
          "market_cap": 706414097373.7339,
          "market_cap_dominance": 41.8173,
          "fully_diluted_market_cap": 783047156496.06,
          "last_updated": "2022-01-31T11:06:00.000Z"
        }
      }
    }
  }
}

当我使用 id="1027" 进行相同的 api 调用时,响应会改变其结构

{
  "status": {
    "timestamp": "2022-01-31T11:46:02.894Z",
    "error_code": 0,
    "error_message": null,
    "elapsed": 28,
    "credit_count": 1,
    "notice": null
  },
  "data": {
    "1027": {
      "id": 1027,
      "name": "Ethereum",
      "symbol": "ETH",
      "slug": "ethereum",
      "num_market_pairs": 5482,
      "date_added": "2015-08-07T00:00:00.000Z",
      "max_supply": null,
      [...]
      "last_updated": "2022-01-31T11:44:00.000Z",
      "quote": {
        "USD": {
          "price": 2535.692637309123,
          "volume_24h": 10427616453.128471,
          "volume_change_24h": -6.6085,
          "percent_change_1h": -0.23965775,
          "percent_change_24h": -3.07033246,
          "percent_change_7d": 12.35705047,
          "percent_change_30d": -31.64459631,
          "percent_change_60d": -44.12893821,
          "percent_change_90d": -42.93608624,
          "market_cap": 302724966788.3792,
          "market_cap_dominance": 17.9889,
          "fully_diluted_market_cap": 302724966788.38,
          "last_updated": "2022-01-31T11:44:00.000Z"
        }
      }
    }
  }
}

在声明结构时是否有可能使用 json:"*any"
现在,如果我用 1 (json:"1") 声明它,它只适用于 BTC

type CoinInfoResponse struct {
    Status struct {
        Timestamp    time.Time   `json:"timestamp"`
        ErrorCode    int         `json:"error_code"`
        ErrorMessage interface{} `json:"error_message"`
        Elapsed      int         `json:"elapsed"`
        CreditCount  int         `json:"credit_count"`
        Notice       interface{} `json:"notice"`
    } `json:"status"`
    Data struct {
        Coin struct {
            ID             int       `json:"id"`
            Name           string    `json:"name"`
            Symbol         string    `json:"symbol"`
            Slug           string    `json:"slug"`
            NumMarketPairs int       `json:"num_market_pairs"`
            DateAdded      time.Time `json:"date_added"`
            Tags           []struct {
                Slug     string `json:"slug"`
                Name     string `json:"name"`
                Category string `json:"category"`
            } `json:"tags"`
            MaxSupply         interface{} `json:"max_supply"`
            CirculatingSupply float64     `json:"circulating_supply"`
            TotalSupply       float64     `json:"total_supply"`
            Platform          struct {
                ID           int    `json:"id"`
                Name         string `json:"name"`
                Symbol       string `json:"symbol"`
                Slug         string `json:"slug"`
                TokenAddress string `json:"token_address"`
            } `json:"platform"`
            IsActive    int       `json:"is_active"`
            CmcRank     int       `json:"cmc_rank"`
            IsFiat      int       `json:"is_fiat"`
            LastUpdated time.Time `json:"last_updated"`
            Quote       struct {
                USD struct {
                    Price            float64   `json:"price"`
                    Volume24H        float64   `json:"volume_24h"`
                    PercentChange1H  float64   `json:"percent_change_1h"`
                    PercentChange24H float64   `json:"percent_change_24h"`
                    PercentChange7D  float64   `json:"percent_change_7d"`
                    PercentChange30D float64   `json:"percent_change_30d"`
                    PercentChange60D float64   `json:"percent_change_60d"`
                    PercentChange90D float64   `json:"percent_change_90d"`
                    MarketCap        float64   `json:"market_cap"`
                    LastUpdated      time.Time `json:"last_updated"`
                } `json:"USD"`
            } `json:"quote"`
        } `json:"1"`
    } `json:"data"`
}

您需要一个自定义解组器。它应该首先解析“ID”,然后解组内部 {...} 对象。

示例 (https://go.dev/play/p/co12qwKBFpc):

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
    "strings"
)

const input = `
{
  "status": {
    "error_code": 100
  },
  "data": {
    "12345": {
      "id": 12345,
      "name": "aaaaa",
      "symbol": "bbbb"
    }
  }
}`

type data struct {
    TagName string
    Coin    struct {
        ID     int    `json:"id"`
        Name   string `json:"name"`
        Symbol string `json:"symbol"`
        // other fileds skipped
    }
}

func (d *data) UnmarshalJSON(b []byte) error {
    idx := bytes.Index(b, []byte(":"))
    // cut "number" and put into TagName
    d.TagName = strings.Trim(string(b[:idx-1]), "{\":\n ")
    // unmarshal internal {} object
    return json.Unmarshal(b[idx+1:len(b)-1], &d.Coin)
}

type Response struct {
    Status struct {
        ErrorCode int `json:"error_code"`
    } `json:"status"`
    Data data
}

func main() {
    var resp Response
    if err := json.Unmarshal([]byte(input), &resp); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%+v\n", resp)
}