英文:
Is there an "any" tag for json unmarshal?
问题
我想调用两次coinmarketcap的API,对于这两次调用,响应几乎相同,只有一个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"
?如果我使用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"`
}
英文:
I want to make two calls to coinmarketcap, and the response is almost the same for both calls, only differs one ID
If I call with id="1" then the repsonse struct will be somenthing like this
{
"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"
}
}
}
}
}
When I make the same api call using id="1027" then the response changes its structure
{
"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"
}
}
}
}
}
Is there any possibility of using json:"*any"
when declaring the struct ?
Now, if I'm declaring it with 1 (json:"1"
) it only works for 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"`
}
答案1
得分: 2
你需要一个自定义的解组器(unmarshaler)。它应该首先解析 "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"`
// 其他字段省略
}
}
func (d *data) UnmarshalJSON(b []byte) error {
idx := bytes.Index(b, []byte(":"))
// 截取 "number" 并放入 TagName
d.TagName = strings.Trim(string(b[:idx-1]), `{"\n `)
// 解组内部的 {} 对象
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)
}
这段代码实现了一个自定义的解组器(UnmarshalJSON),它首先解析 "ID",然后将内部的 {...}
对象解组到相应的结构体字段中。
英文:
You need a custom unmarshaler. It should parse "ID" first, then to unmarshal internal {...}
object.
Example (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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论