Json Unmarshaling in Golang

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

Json Unmarshaling in Golang

问题

我想在golang中解析以下json。我无法访问内部数据。最好的方法是什么?

  1. {
  2. "1": {
  3. "enabled": 1,
  4. "pct": 0.5
  5. },
  6. "2": {
  7. "enabled": 1,
  8. "pct": 0.6
  9. },
  10. "3": {
  11. "enabled": 1,
  12. "pct": 0.2
  13. },
  14. "4": {
  15. "enabled": 1,
  16. "pct": 0.1
  17. }
  18. }

我使用了以下代码:

  1. type regs struct {
  2. enabled bool `json:"enabled,omitempty"`
  3. pct float64 `json:"pct,omitempty"`
  4. }
  5. var r map[string]regs
  6. if err := json.Unmarshal([]byte(jStr), &r); err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Printf("%+v\n", r)

但是我看不到结构体内部的值。
结果:map[1:{enabled:false pct:0} 2:{enabled:false pct:0} 3:{enabled:false pct:0} 4:{enabled:false pct:0}]

英文:

I want to unmarshal the followng json in golang. I am not able to access the inner data .
What is the best way to do it?

  1. {
  2. "1": {
  3. "enabled": 1,
  4. "pct": 0.5
  5. },
  6. "2": {
  7. "enabled": 1,
  8. "pct": 0.6
  9. },
  10. "3": {
  11. "enabled": 1,
  12. "pct": 0.2
  13. },
  14. "4": {
  15. "enabled": 1,
  16. "pct": 0.1
  17. }
  18. }

I use

  1. type regs struct {
  2. enabled bool `json:"enabled,omitempty"`
  3. pct float64 `json:"pct,omitempty"`
  4. }
  5. var r map[string]regs
  6. if err := json.Unmarshal([]byte(jStr), &r); err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Printf("%+v\n", r)

but i dont see the values inside the struct.
Result: map[1:{enabled:false pct:0} 2:{enabled:false pct:0} 3:{enabled:false pct:0} 4:{enabled:false pct:0}]

答案1

得分: 1

对于编组和解组,您应该将结构字段定义为导出字段,并且目标应该是regs的映射。
此外,bool类型对于Enabled字段无效,您应该将其更改为int类型。

  1. type regs struct {
  2. Enabled int `json:"enabled,omitempty"`
  3. Pct float64 `json:"pct,omitempty"`
  4. }
  5. func main() {
  6. a := `{
  7. "1": {
  8. "enabled": 1,
  9. "pct": 0.5
  10. },
  11. "2": {
  12. "enabled": 1,
  13. "pct": 0.6
  14. },
  15. "3": {
  16. "enabled": 1,
  17. "pct": 0.2
  18. },
  19. "4": {
  20. "enabled": 1,
  21. "pct": 0.1
  22. }
  23. }`
  24. dest := make(map[string]regs)
  25. json.Unmarshal([]byte(a), &dest)
  26. fmt.Println(dest)
  27. }

输出将为:

  1. map[1:{1 0.5} 2:{1 0.6} 3:{1 0.2} 4:{1 0.1}]
英文:

For Marshaling and Unmarshaling, you should define struct field as exported field, also the destination should be map of regs.
Also the bool type is not valid for the Enabled and you should change it to int

  1. type regs struct {
  2. Enabled int `json:"enabled,omitempty"`
  3. Pct float64 `json:"pct,omitempty"`
  4. }
  5. func main() {
  6. a := `{
  7. "1": {
  8. "enabled": 1,
  9. "pct": 0.5
  10. },
  11. "2": {
  12. "enabled": 1,
  13. "pct": 0.6
  14. },
  15. "3": {
  16. "enabled": 1,
  17. "pct": 0.2
  18. },
  19. "4": {
  20. "enabled": 1,
  21. "pct": 0.1
  22. }
  23. }`
  24. dest := make(map[string]regs)
  25. json.Unmarshal([]byte(a), &dest)
  26. fmt.Println(dest)
  27. }

The output will be:

  1. map[1:{1 0.5} 2:{1 0.6} 3:{1 0.2} 4:{1 0.1}]

huangapple
  • 本文由 发表于 2022年2月18日 02:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71163766.html
匿名

发表评论

匿名网友

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

确定