英文:
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"`
}
我想要遍历response
来获取结构体中某些元素的值。
- 如何获取
Symbols => Symbol
的值? - 如何获取
Symbols => Filters => MaxPrice
的值?
我知道timezone
可以正常工作,但不确定如何获取symbol
和maxPrice
的值。
var data []response
for _, r := range data {
fmt.Println("timezone: ", r.Timezone)
fmt.Println("symbol: ", r.Symbols.Symbol) // 如何获取?
fmt.Println("maxPrice: ", r.Symbols.Filters.MaxPrice) // 如何获取?
}
并且看起来像这样:
{
"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
}
]
}
]
}
如何遍历这个结构体以获取symbol
和maxPrice
的值呢?
英文:
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
- How do i get the value of
Symbols => Symbol
? - 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
答案1
得分: 2
你的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
英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论