如何使用Go将JSON文件解析为结构体

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

How Do I Parse a JSON file into a struct with Go

问题

我正在尝试通过创建一个JSON文件并将其解析为结构体来配置我的Go程序:

  1. var settings struct {
  2. serverMode bool
  3. sourceDir string
  4. targetDir string
  5. }
  6. func main() {
  7. // 然后是配置文件设置
  8. configFile, err := os.Open("config.json")
  9. if err != nil {
  10. printError("打开配置文件", err.Error())
  11. }
  12. jsonParser := json.NewDecoder(configFile)
  13. if err = jsonParser.Decode(&settings); err != nil {
  14. printError("解析配置文件", err.Error())
  15. }
  16. fmt.Printf("%v %s %s", settings.serverMode, settings.sourceDir, settings.targetDir)
  17. return
  18. }

config.json文件:

  1. {
  2. "serverMode": true,
  3. "sourceDir": ".",
  4. "targetDir": "."
  5. }

程序编译和运行时没有任何错误,但打印语句输出:

  1. false
  2. false和两个空字符串)
  3. 我也尝试过使用`json.Unmarshal(..)`,但结果相同。
  4. 如何以填充结构体值的方式解析JSON
  5. <details>
  6. <summary>英文:</summary>
  7. I&#39;m trying to configure my Go program by creating a JSON file and parsing it into a struct:
  8. var settings struct {
  9. serverMode bool
  10. sourceDir string
  11. targetDir string
  12. }
  13. func main() {
  14. // then config file settings
  15. configFile, err := os.Open(&quot;config.json&quot;)
  16. if err != nil {
  17. printError(&quot;opening config file&quot;, err.Error())
  18. }
  19. jsonParser := json.NewDecoder(configFile)
  20. if err = jsonParser.Decode(&amp;settings); err != nil {
  21. printError(&quot;parsing config file&quot;, err.Error())
  22. }
  23. fmt.Printf(&quot;%v %s %s&quot;, settings.serverMode, settings.sourceDir, settings.targetDir)
  24. return
  25. }
  26. The config.json file:
  27. {
  28. &quot;serverMode&quot;: true,
  29. &quot;sourceDir&quot;: &quot;.&quot;,
  30. &quot;targetDir&quot;: &quot;.&quot;
  31. }
  32. The Program compiles and runs without any errors, but the print statement outputs:
  33. false
  34. (false and two empty strings)
  35. I&#39;ve also tried with `json.Unmarshal(..)` but had the same result.
  36. How do I parse the JSON in a way that fills the struct values?
  37. </details>
  38. # 答案1
  39. **得分**: 53
  40. 你没有导出你的结构体元素。它们都以小写字母开头。
  41. var settings struct {
  42. ServerMode bool `json:"serverMode"`
  43. SourceDir string `json:"sourceDir"`
  44. TargetDir string `json:"targetDir"`
  45. }
  46. 将结构体元素的首字母改为大写以导出它们。JSON编码器/解码器不会使用未导出的结构体元素。
  47. <details>
  48. <summary>英文:</summary>
  49. You&#39;re not exporting your struct elements. They all begin with a lower case letter.
  50. var settings struct {
  51. ServerMode bool `json:&quot;serverMode&quot;`
  52. SourceDir string `json:&quot;sourceDir&quot;`
  53. TargetDir string `json:&quot;targetDir&quot;`
  54. }
  55. Make the first letter of your stuct elements upper case to export them. The JSON encoder/decoder wont use struct elements which are not exported.
  56. </details>

huangapple
  • 本文由 发表于 2013年5月22日 07:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/16681003.html
匿名

发表评论

匿名网友

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

确定