Go – JSON Decoder not initializing my struct

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

Go - JSON Decoder not initializing my struct

问题

我正在尝试解码通过http.Get获取的一些JSON数据。然而,当我使用fmt.Println检查我初始化的结构体时,它们总是为空的。

我怀疑这是因为我的结构体的结构与返回的JSON不一致,但我不确定如何修复它。总的来说,我对解码器的工作原理并不十分了解。

这是JSON数据:

{
"response":[
{
"list": {
"category":"(noun)",
"synonyms":"histrion|player|thespian|role player|performer|performing artist"
}
},
{
"list": {
"category":"(noun)",
"synonyms":"doer|worker|person|individual|someone|somebody|mortal|soul"
}
}
]
}

我目前尝试过的方法如下:

type SynonymResponse struct {
lists []SynonymList
}

type SynonymList struct {
category string
synonyms string
}

var synonyms SynonymResponse;
dec := json.NewDecoder(response.Body)
err := dec.Decode(&synonyms)
if err != nil {
log.Fatal(err)
}
fmt.Println(synonyms)

编辑:根据@Leo的答案和@JimB的提示,我的尝试存在两个问题。以下是正确的结构体设置,尽管如Leo所指出的,这将为空:

type SynonymResponses struct {
resp []SynonymResponse
}

type SynonymResponse struct {
listo SynonymList
}

type SynonymList struct {
cat string
syns string
}

英文:

I am trying to decode some JSON retrieved via http.Get. However, when I check the structs I initialize with fmt.Println, they are always empty.

I suspect it is because my struct's structure does not agree with the returned JSON, but I am not sure how to fix it. In general, I am not exactly quite sure how the decoder works.

This is the JSON:

{
  "response":[
    {
      "list": {
        "category":"(noun)",
        "synonyms":"histrion|player|thespian|role player|performer|performing artist"
      }
    },
    {
      "list": {
        "category":"(noun)",
        "synonyms":"doer|worker|person|individual|someone|somebody|mortal|soul"
      }
    }
  ]
}

Here is what i have tried so far:

type SynonymResponse struct {
    lists []SynonymList
}

type SynonymList struct {
    category string
    synonyms string
}

var synonyms SynonymResponse;
dec := json.NewDecoder(response.Body)
err := dec.Decode(&synonyms)
if err != nil {
	log.Fatal(err)
}
fmt.Println(synonyms)

EDIT: Per @Leo's answer and @JimB's hint, there are two issues with my attempt. Below is the proper set of structs, though as Leo pointed out, this will be empty:

type SynonymResponses struct {
    resp []SynonymResponse
}

type SynonymResponse struct {
	listo SynonymList
}

type SynonymList struct {
	cat string
	syns string
} 

答案1

得分: 6

为了使你的JSON能够被解码器识别,你的结构体中的字段必须是可导出的。

这意味着你需要将字段名大写。如果你在字段上有自定义命名 -> JSON 转换,你可以在结构体上添加 JSON 标签。

以下代码将修复你的问题:

type SynonymResponse struct {
    Lists []SynonymList `json:"response"`
}

type SynonymList struct {
    Category string `json:"category"`
    Synonyms string `json:"synonyms"`
}
英文:

In order for your JSON to be picked up by the decoder, the fields in your struct must be exported.

This means you need you capitalize the field names. If you have custom naming on your fields -> json conversion, you can add json tags to your structs.

This will fix your issue:

type SynonymResponse struct {
    Lists []SynonymList `json:"response"`
}

type SynonymList struct {
    Category string `json:"category"`
    Synonyms string `json:"synonyms"`
}

huangapple
  • 本文由 发表于 2016年1月10日 01:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/34696852.html
匿名

发表评论

匿名网友

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

确定