英文:
golang how i can get value from []interface {} type
问题
我有一个名为result["error_type"]的变量,类型为[]interface{},值为[map[reason:map[phone:empty] send_at:1.636697291e+09 status:error]]。我想知道如何从类型[]interface{}中获取值。
例如:result["error_type"]["128"]["reason"]["phone"]
我从以下代码中获取了这个类型:
var result map[string]interface{}
json.NewDecoder(r.Body).Decode(&result)
r.Body中包含了JSON数据:
{
"offer_name":"EbcBankruptcy",
"offer_id":"288",
"partner_name":"середов",
"partner_id":"1",
"type_system":"gb",
"status":"success",
"date_request":"2021-01-02 11:03",
"bank_name":"alfa",
"bank_id":"1",
"type_product":"1",
"error_type":{"128": [{"reason": {"phone": "Отсутствует обязательное поле номер телефона"}, "status": "error", "send_at": 1636697291}], "200": [{"reason": {"phone": "Отсутствует обязательное поле номер телефона"}, "status": "error", "send_at": 1636697281}]},
"request_id":"1"
}
另外,我没有为json.NewDecoder解析创建error_type的结构,因为我不知道json中error_type的id会是什么(128、200、300)。
我尝试获取值:
test["reason"]["phone"]
但是,它不起作用。
我还尝试将其转换为:
map[string]interface{}
但也不起作用。
英文:
i have var (name result["error_type"]) with type
[]interface {}
and value
[map[reason:map[phone:empty] send_at:1.636697291e+09 status:error]]
how i cat get value from type []interface {}
example result["error_type"]["128"]["reason"]["phone"]
this type i got from
var result map[string]interface{}
json.NewDecoder(r.Body).Decode(&result)
r.Body has Json
{
"offer_name":"EbcBankruptcy",
"offer_id":"288",
"partner_name":"середов",
"partner_id":"1",
"type_system":"gb",
"status":"success",
"date_request":"2021-01-02 11:03",
"bank_name":"alfa",
"bank_id":"1",
"type_product":"1",
"error_type":{"128": [{"reason": {"phone": "Отсутствует обязательное поле номер телефона"}, "status": "error", "send_at": 1636697291}], "200": [{"reason": {"phone": "Отсутствует обязательное поле номер телефона"}, "status": "error", "send_at": 1636697281}]},
"request_id":"1"
}
also i dont create structure error_type for json.NewDecoder parse because i don
t know what kind id will be in error_type in json (128, 200, 300)
i try get value
test["reason"]["phone"]
but, its not work
also cast to
map[string]interface{}
its not work
答案1
得分: 1
根据问题的理解,我理解的是你可以按照以下格式对数据进行格式化。
type Payload struct {
OfferName string `json:"offer_name"`
OfferID string `json:"offer_id"`
PartnerName string `json:"partner_name"`
PartnerID string `json:"partner_id"`
TypeSystem string `json:"type_system"`
Status string `json:"status"`
DateRequest string `json:"date_request"`
BankName string `json:"bank_name"`
BankID string `json:"bank_id"`
TypeProduct string `json:"type_product"`
// 在这里你可以使用错误数据的类型映射数组
ErrorType map[string][]ErrorData `json:"error_type"`
RequestID string `json:"request_id"`
}
type ErrorData struct {
Reason Reason `json:"reason"`
Status string `json:"status"`
SendAt int `json:"send_at"`
}
type Reason struct {
Phone string `json:"phone"`
}
使用上述代码,你可以将数据解组为:
fmt.Printf("%+v", p.ErrorType["128"][0].Reason)
如果你无法知道映射的键,你仍然可以遍历映射值并获取数据。
这是 Playground 的链接:https://go.dev/play/p/oTF0JUwOj0D
英文:
As far as the understanding from the question, what I understand is you can format the data as following.
type Payload struct {
OfferName string `json:"offer_name"`
OfferID string `json:"offer_id"`
PartnerName string `json:"partner_name"`
PartnerID string `json:"partner_id"`
TypeSystem string `json:"type_system"`
Status string `json:"status"`
DateRequest string `json:"date_request"`
BankName string `json:"bank_name"`
BankID string `json:"bank_id"`
TypeProduct string `json:"type_product"`
// you can use the type map of array of error data here
ErrorType map[string][]ErrorData `json:"error_type"`
RequestID string `json:"request_id"`
}
type ErrorData struct {
Reason Reason `json:"reason"`
Status string `json:"status"`
SendAt int `json:"send_at"`
}
type Reason struct {
Phone string `json:"phone"`
}
With the following you can unmarshal the data to
fmt.Printf("%+v", p.ErrorType["128"][0].Reason)
> In the case you are unable to know the keys of the map, you can still range over the map values and get the data.
Here is the playground link https://go.dev/play/p/oTF0JUwOj0D
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论