在Golang中进行JSON解析时获取可空对象。

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

getting nullable object on json unmarshal in golang

问题

以下是翻译好的内容:

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

type student struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Standard string `json:"std"`
}

type cls struct {
	st student `json:"cls"`
}

func getValues() (cls, error) {
	var clss cls
	dataBytes, err := ioutil.ReadFile("studentclass.json")
	if err != nil {
		fmt.Printf("文件错误:%v\n", err)
		os.Exit(1)
	}
	err = json.Unmarshal(dataBytes, &clss)
	if err != nil {
		fmt.Errorf("%s", err)
	}
	return clss, err
}

func main() {
	s, err := getValues()
	fmt.Printf("%#v\n", s)
	fmt.Println(err)
}
{
	"cls": {
		"id": "1",
		"name": "test",
		"std": "0"
	}
}

当我运行 go run jsonTestParse.go 时,它给出了以下输出:

main.cls{st:main.student{ID:"", Name:"", Standard:""}}
<nil>

请帮我解释为什么我得到了这个空对象:

main.cls{st:main.student{ID:"", Name:"", Standard:""}}

而不是这个:

main.cls{st:main.student{ID:"1", Name:"test", Standard:"0"}}

另外,如何获取这些值会很有帮助?

英文:

> Go code(jsonTestParse.go)
>
> (this is just a test example I made, please don't argue that I should
> use list of students in cls struct)

package main

import (
    &quot;encoding/json&quot;
    &quot;fmt&quot;
    &quot;io/ioutil&quot;
    &quot;os&quot;
)

type student struct {
    ID       string `json:&quot;id&quot;`
    Name     string `json:&quot;name&quot;`
    Standard string `json:&quot;std&quot;`
}

type cls struct {
    st student `json:&quot;cls&quot;`
}

func getValues() (cls, error) {
    var clss cls
    dataBytes, err := ioutil.ReadFile(&quot;studentclass.json&quot;)
    if err != nil {
        fmt.Printf(&quot;File error: %v\n&quot;, err)
        os.Exit(1)
    }
    err = json.Unmarshal(dataBytes, &amp;clss)
    if err != nil {
        fmt.Errorf(&quot;%s&quot;, err)
    }
    return clss, err
}

func main() {
    s, err := getValues()
    fmt.Printf(&quot;%#v\n&quot;, s)
    fmt.Println(err)
}

> Json File (studentclass.json)

{
    &quot;cls&quot;: {
        &quot;id&quot;: &quot;1&quot;,
        &quot;name&quot;: &quot;test&quot;,
        &quot;std&quot;: &quot;0&quot;
    }
}

When I run this code with go run jsonTestParse.go it gives me this output:

main.cls{st:main.student{ID:&quot;&quot;, Name:&quot;&quot;, Standard:&quot;&quot;}}
&lt;nil&gt;

Please help me why I'm getting this blank object

main.cls{st:main.student{ID:&quot;&quot;, Name:&quot;&quot;, Standard:&quot;&quot;}}

instead of this

main.cls{st:main.student{ID:&quot;1&quot;, Name:&quot;test&quot;, Standard:&quot;0&quot;}}

Plus It would be great to help on how to get these values?

答案1

得分: 3

这是因为你的 cls 结构体中嵌入了私有结构体(小写未导出字段) student st,将其改为导出字段应该可以解决问题,即:

type cls struct {
    // St 字段将被反序列化
    St student `json:"cls"`
}

playground 中查看。

英文:

That is because your cls struct has embedded private struct (lower case unexported field) of student st, change to exported field should work, that is:

type cls struct {
    // St field will be unmarshalled
    St student `json:&quot;cls&quot;`
}

See in playground

huangapple
  • 本文由 发表于 2016年12月6日 19:13:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/40993987.html
匿名

发表评论

匿名网友

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

确定