Home > front end >  How do i get values of elements in slice of interface?
How do i get values of elements in slice of interface?

Time:02-05

I have the following struct

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"`
}

I will like to iterate over response to get the values of certain elements deep into the struct

  1. How do i get the value of Symbols => Symbol?
  2. How do i get the value of Symbols => Filters => MaxPrice?

I know timezone works fine but not sure how to get symbol and maxPrice

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???
}

and looks something like this

{
    "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
                }
            ]
        }
    ]
}

How do i iterate over the

CodePudding user response:

Your target structure for un-marshalling your JSON is incorrectly defined. If you take a look at your JSON structure, its an object type with a symbols being an array type. The equivalent Go representation would be a type struct with a slice of symbol records.

Your function should be written as

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

  •  Tags:  
  • Related