如何获取界面切片中元素的值?

How do i get values of elements in slice of interface?

我有以下结构

type response struct {
        Symbols    []struct {
                Status                     string   `json:"status"`
                Symbol                     string   `json:"symbol"`
                BaseAssetPrecision      int64  `json:"baseAssetPrecision"`
                BaseCommissionPrecision int64  `json:"baseCommissionPrecision"`
                Filters                 []struct {
                        FilterType string  `json:"filterType"`
                        MaxPrice   float64 `json:"maxPrice,string"`
                        MinPrice   float64 `json:"minPrice,string"`
                        TickSize   float64 `json:"tickSize,string"`
                } `json:"filters"`
        } `json:"symbols"`
        Timezone string `json:"timezone"`
}

我想遍历响应以获得深入结构的某些元素的值

  1. 如何获取 Symbols => Symbol 的值?
  2. 如何获取 Symbols => Filters => MaxPrice 的值?

我知道 timezone 工作正常但不确定如何获得 symbolmaxPrice

var data []response
    
for _, r := range data {
    fmt.Println("timezone: ", r.Timezone)
    fmt.Println("symbol: ", r.Symbols.Symbol) // how???
    fmt.Println("maxPrice: ", r.Symbols.Filters.MaxPrice) // how???
}

看起来像这样

{
    "timezone": "UTC",
    "symbols": [{
            "symbol": "TESLA",
            "status": "TRADING",
            "baseAssetPrecision": 8,
            "baseCommissionPrecision": 8,
            "filters": [{
                    "filterType": "PRICE_FILTER",
                    "minPrice": "0.00000100",
                    "maxPrice": "922327.00000000",
                    "tickSize": "0.00000100"
                },
                {
                    "filterType": "PERCENT_PRICE",
                    "multiplierUp": "5",
                    "multiplierDown": "0.2",
                    "avgPriceMins": 5
                },
                {
                    "filterType": "LOT_SIZE",
                    "minQty": "0.00010000",
                    "maxQty": "100000.00000000",
                    "stepSize": "0.00010000"
                },
                {
                    "filterType": "MIN_NOTIONAL",
                    "minNotional": "0.00010000",
                    "applyToMarket": true,
                    "avgPriceMins": 5
                },
                {
                    "filterType": "ICEBERG_PARTS",
                    "limit": 10
                },
                {
                    "filterType": "MARKET_LOT_SIZE",
                    "minQty": "0.00000000",
                    "maxQty": "891.04020423",
                    "stepSize": "0.00000000"
                },
                {
                    "filterType": "MAX_NUM_ORDERS",
                    "maxNumOrders": 200
                },
                {
                    "filterType": "MAX_NUM_ALGO_ORDERS",
                    "maxNumAlgoOrders": 5
                }
            ]
        },
        {
            "symbol": "AAPL",
            "status": "TRADING",
            "baseAssetPrecision": 8,
            "baseCommissionPrecision": 8,
            "filters": [{
                    "filterType": "PRICE_FILTER",
                    "minPrice": "0.00000100",
                    "maxPrice": "922327.00000000",
                    "tickSize": "0.00000100"
                },
                {
                    "filterType": "PERCENT_PRICE",
                    "multiplierUp": "5",
                    "multiplierDown": "0.2",
                    "avgPriceMins": 5
                },
                {
                    "filterType": "LOT_SIZE",
                    "minQty": "0.00010000",
                    "maxQty": "100000.00000000",
                    "stepSize": "0.00010000"
                },
                {
                    "filterType": "MIN_NOTIONAL",
                    "minNotional": "0.00010000",
                    "applyToMarket": true,
                    "avgPriceMins": 5
                },
                {
                    "filterType": "ICEBERG_PARTS",
                    "limit": 10
                },
                {
                    "filterType": "MARKET_LOT_SIZE",
                    "minQty": "0.00000000",
                    "maxQty": "891.04020423",
                    "stepSize": "0.00000000"
                },
                {
                    "filterType": "MAX_NUM_ORDERS",
                    "maxNumOrders": 200
                },
                {
                    "filterType": "MAX_NUM_ALGO_ORDERS",
                    "maxNumAlgoOrders": 5
                }
            ]
        }
    ]
}

我如何遍历

您的 un-marshalling 和 JSON 的目标结构定义不正确。如果您查看 JSON 结构,它是一个对象类型,其中 symbols 是一个数组类型。等效的 Go 表示形式是 type struct 和一段符号记录。

你的函数应该写成

var data response
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
    panic(err)
}
for _, rec := range data.Symbols {
    fmt.Println(rec.Symbol)
    for _, filter := range rec.Filters {
        fmt.Printf("%f\n", filter.MaxPrice)
    }
}

https://go.dev/play/p/5oxJfYx_61M