Golang类型转换。将接口转换为结构体。

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

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, &amp;data)
fmt.Println(&quot;data :&quot;, 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, &amp;data)
fmt.Println(&quot;data :&quot;, 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:&quot;id&quot;`
    Guid string `json:&quot;guid&quot;`
    Name string `json:&quot;name&quot;`
}

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:&quot;id&quot;`
    guid string `json:&quot;guid&quot;`
    name string `json:&quot;name&quot;`
}
type Result struct {
    Data []ResultStruct `json:&quot;data&quot;`
}

response := httpClient.Do(request)
var data Result
decErr := json.NewDecoder(response.body).Decode(&amp;data)
fmt.Println(decErr, data)

This should Unmarshal the data into array.

huangapple
  • 本文由 发表于 2016年2月24日 17:26:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/35598102.html
匿名

发表评论

匿名网友

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

确定