英文:
Convert interface{} to struct in Golang
问题
我对Go语言非常陌生,正在努力理解所有不同的类型以及如何使用它们。我有一个包含以下内容的接口(最初是从一个JSON文件中获取的):
[map[item:electricity transform:{fuelType}] map[transform:{fuelType} item:gas]]
我还有以下结构体:
type urlTransform struct {
item string
transform string
}
我不知道如何将接口数据传递到结构体中;我确定这很愚蠢,但我已经尝试了一整天。非常感谢任何帮助。
英文:
I am very new to Go and am trying to get my head around all the different types and how to use them. I have an interface with the following (which was originally in a json file):
[map[item:electricity transform:{fuelType}] map[transform:{fuelType} item:gas]]
and I have the following struct
type urlTransform struct {
item string
transform string
}
I have no idea how to get the interface data into the struct; I'm sure this is really stupid, but I have been trying all day. Any help would be greatly appreciated.
答案1
得分: 7
将JSON直接解码为所需的类型,而不是解码为interface{}
。
声明与JSON数据结构相匹配的类型。对于JSON对象,请使用结构体,对于JSON数组,请使用切片:
type transform struct {
// 根据问题中的信息,无法填写此部分。
}
type urlTransform struct {
Item string
Transform transform
}
var transforms []urlTransform
字段名称必须是导出的(以大写字母开头)。
将JSON解组为声明的值:
err := json.Unmarshal(data, &transforms)
或者
err := json.NewDecoder(reader).Decode(&transforms)
英文:
Decode the JSON directly to types you want instead of decoding to an interface{}
.
Declare types that match the structure of your JSON data. Use structs for JSON objects and slices for JSON arrays:
type transform struct {
// not enough information in question to fill this in.
}
type urlTransform struct {
Item string
Transform transform
}
var transforms []urlTransform
The field names must be exported (start with uppercase letter).
Unmarshal the JSON to the declared value:
err := json.Unmarshal(data, &transforms)
or
err := json.NewDecoder(reader).Decode(&transforms)
答案2
得分: 3
从你的回答中可以看到,这是一个包含map
的array
。
从中获取值的一种方法是:
values := yourResponse[0].(map[string]interface{}) // 将第一个索引转换为包含接口值的map
transform := urlTransform{}
transform.Item = values["item"].(string) // 将item值转换为字符串
transform.Transform = values["transform"].(string)
// 其他字段的处理...
如上所示,我通过使用map
来获取值,并将值转换为适当的类型,例如string
。
你可以根据需要将其转换为适当的类型,如int
、bool
或其他类型。但是这种方法比较繁琐,因为你需要逐个获取值并将其分配给你的字段结构。
英文:
From your response : [map[item:electricity transform:{fuelType}] map[transform:{fuelType} item:gas]]
.
As you can see here this is a an array
that has map
in it.
One way to get the value from this is :
values := yourResponse[0].(map[string]interface{}). // convert first index to map that has interface value.
transform := urlTransform{}
transform.Item = values["item"].(string) // convert the item value to string
transform.Transform = values["transform"].(string)
//and so on...
as you can see from the code above I'm getting the the value using map. And convert the value to the appropriate type in this case is string
.
You can convert it to appropriate type like int
or bool
or other type. but this approach is painful as you need to get the value one bye one and assign it your your field struct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论