英文:
golang type conversion. interface to struct
问题
我不习惯使用golang
。
当我发出请求时,我得到了以下日志。
我想解析这个日志并存储到struct
类型中。
有人可以告诉我如何做吗?
提前感谢。
例如:
type ResultStruct struct{
id int
guid string
name string
}
for k, v := range data {
fmt.Print(v.id) fmt.Print(v.guid) fmt.Print(v.name)
}
[日志]
> data: [map[id:90001 guid:a name:test1] map[guid:b name:test2 id:90002] map[name:test3 id:90003 guid:c]]
<b>[源代码]</b>
response := httpClient.Do(request)
var data interface{}
rawdata, err := ioutil.ReadAll(response.body)
json.Unmarshal(rawdata, &data)
fmt.Println("data :", data)
英文:
I am not used to golang
.
When I requested, I got the below log.
I would like to parse the log and store into struct
type.
Someone lets me know how to do?
Thanks in advance.
for example :
type ResultStruct struct{
id int
guid string
name string
}
for k, v := range data {
fmt.Print(v.id) fmt.Print(v.guid) fmt.Print(v.name)
}
[log]
> data: [map[id:90001 guid:a name:test1] map[guid:b name:test2 id:90002] map[name:test3 id:90003 guid:c]]
<b>[source]</b>
response := httpClient.Do(request)
var data interface{}
rawdata, err := ioutil.ReadAll(response.body)
json.Unmarshal(rawdata, &data)
fmt.Println("data :", data)
答案1
得分: 2
这是一个常见的新手 Go 程序员的错误。
由于语言设计的原因,json.Unmarshal
只能将数据解析到导出的 字段中。
只需将每个字段名的首字母大写以导出它们。如果需要,你还可以添加字段标签来告诉 json.Marshal
使用哪个键名。这仅在你打算使用 json.Marshal
时才需要。
type ResultStruct struct{
Id int `json:"id"`
Guid string `json:"guid"`
Name string `json:"name"`
}
引用encoding/json
包的说明:
> 要将 JSON 解析为结构体,Unmarshal
会将传入对象的键与 Marshal
使用的键进行匹配(可以是结构体字段名或其标签),优先选择精确匹配,但也接受不区分大小写的匹配。Unmarshal
只会设置结构体的导出字段。
英文:
It is a common mistake for new Go programmers.
Because of language design, json.Unmarshal
can only marshal into exported fields.
Simply capitalize the first letter of each field name to export them. Optionally you may add field tags to tell json.Marshal what key-name to use. This is only needed if you are going to use json.Marshal
.
type ResultStruct struct{
Id int `json:"id"`
Guid string `json:"guid"`
Name string `json:"name"`
}
To quote the encoding/json
package:
> To unmarshal JSON into a struct, Unmarshal matches incoming object
> keys to the keys used by Marshal (either the struct field name or its
> tag), preferring an exact match but also accepting a case-insensitive
> match. Unmarshal will only set exported fields of the struct.
答案2
得分: 0
你正在获取一个数组,使用encoding/json
包对其进行解组。
type ResultStruct struct {
id int `json:"id"`
guid string `json:"guid"`
name string `json:"name"`
}
type Result struct {
Data []ResultStruct `json:"data"`
}
response := httpClient.Do(request)
var data Result
decErr := json.NewDecoder(response.body).Decode(&data)
fmt.Println(decErr, data)
这样应该可以将数据解组成数组。
英文:
You are getting an Array, Unmarshal it using the encoding/json
package.
type ResultStruct struct {
id int `json:"id"`
guid string `json:"guid"`
name string `json:"name"`
}
type Result struct {
Data []ResultStruct `json:"data"`
}
response := httpClient.Do(request)
var data Result
decErr := json.NewDecoder(response.body).Decode(&data)
fmt.Println(decErr, data)
This should Unmarshal the data into array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论