欢迎光临
我们一直在努力

json unmarshal 是否有“any”标签?

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《JSON unmarshal 是否有“any”标签?》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习

问题内容

我想对 coinmarketcap 进行两次调用,两次调用的响应几乎相同,只有一个 id 不同

如果我使用 id=”1″ 进行调用,那么响应结构将是这样的

{
  "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("%+vn", resp)
}

本篇关于《json unmarshal 是否有“any”标签?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于golang的相关知识,请关注编程网公众号!

赞(0) 打赏
未经允许不得转载:码农资源网 » json unmarshal 是否有“any”标签?
分享到

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册