如何获取接口切片中元素的值?

huangapple go评论98阅读模式
英文:

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来获取结构体中某些元素的值。

  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) // 如何获取?
	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
				}
			]
		}
	]
}

如何遍历这个结构体以获取symbolmaxPrice的值呢?

英文:

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

答案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)
	}
}

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

huangapple
  • 本文由 发表于 2022年2月5日 17:20:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/70996672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定