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

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

getting nullable object on json unmarshal in golang

问题

以下是翻译好的内容:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. )
  8. type student struct {
  9. ID string `json:"id"`
  10. Name string `json:"name"`
  11. Standard string `json:"std"`
  12. }
  13. type cls struct {
  14. st student `json:"cls"`
  15. }
  16. func getValues() (cls, error) {
  17. var clss cls
  18. dataBytes, err := ioutil.ReadFile("studentclass.json")
  19. if err != nil {
  20. fmt.Printf("文件错误:%v\n", err)
  21. os.Exit(1)
  22. }
  23. err = json.Unmarshal(dataBytes, &clss)
  24. if err != nil {
  25. fmt.Errorf("%s", err)
  26. }
  27. return clss, err
  28. }
  29. func main() {
  30. s, err := getValues()
  31. fmt.Printf("%#v\n", s)
  32. fmt.Println(err)
  33. }
  1. {
  2. "cls": {
  3. "id": "1",
  4. "name": "test",
  5. "std": "0"
  6. }
  7. }

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

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

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

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

而不是这个:

  1. 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)

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;io/ioutil&quot;
  6. &quot;os&quot;
  7. )
  8. type student struct {
  9. ID string `json:&quot;id&quot;`
  10. Name string `json:&quot;name&quot;`
  11. Standard string `json:&quot;std&quot;`
  12. }
  13. type cls struct {
  14. st student `json:&quot;cls&quot;`
  15. }
  16. func getValues() (cls, error) {
  17. var clss cls
  18. dataBytes, err := ioutil.ReadFile(&quot;studentclass.json&quot;)
  19. if err != nil {
  20. fmt.Printf(&quot;File error: %v\n&quot;, err)
  21. os.Exit(1)
  22. }
  23. err = json.Unmarshal(dataBytes, &amp;clss)
  24. if err != nil {
  25. fmt.Errorf(&quot;%s&quot;, err)
  26. }
  27. return clss, err
  28. }
  29. func main() {
  30. s, err := getValues()
  31. fmt.Printf(&quot;%#v\n&quot;, s)
  32. fmt.Println(err)
  33. }

> Json File (studentclass.json)

  1. {
  2. &quot;cls&quot;: {
  3. &quot;id&quot;: &quot;1&quot;,
  4. &quot;name&quot;: &quot;test&quot;,
  5. &quot;std&quot;: &quot;0&quot;
  6. }
  7. }

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

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

Please help me why I'm getting this blank object

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

instead of this

  1. 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,将其改为导出字段应该可以解决问题,即:

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

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:

  1. type cls struct {
  2. // St field will be unmarshalled
  3. St student `json:&quot;cls&quot;`
  4. }

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:

确定