在Golang中出现JSON解析错误

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

Error with JSON parsing in Golang

问题

我开发了这段代码:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "encoding/json"
  6. )
  7. type Client struct{
  8. host string
  9. key string
  10. secrete string
  11. username string
  12. password string
  13. }
  14. type Config struct{
  15. Client []Client
  16. }
  17. func main(){
  18. content, err := ioutil.ReadFile("conf2.json")
  19. if err!=nil{
  20. fmt.Print("Error:",err)
  21. }
  22. var conf Config
  23. err=json.Unmarshal(content, &conf)
  24. if err!=nil{
  25. fmt.Print("Error:",err)
  26. }
  27. json.Unmarshal(content, &conf)
  28. fmt.Println(conf.Client[0].host)
  29. }

我想解析并打印出我的 JSON 中的第一个主机详细信息,JSON 的内容如下:

  1. {
  2. "Client" :
  3. [
  4. {"host":"192.168.1.2"},
  5. {"key":"abcdf"},
  6. {"secrete":"9F6w"},
  7. {"username":"user"},
  8. {"password":"password"}
  9. ]
  10. }

但是我得到了一个空字符串。有人知道原因吗?

英文:

I develop this code:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "encoding/json"
  6. )
  7. type Client struct{
  8. host string
  9. key string
  10. secrete string
  11. username string
  12. password string
  13. }
  14. type Config struct{
  15. Client []Client
  16. }
  17. func main(){
  18. content, err := ioutil.ReadFile("conf2.json")
  19. if err!=nil{
  20. fmt.Print("Error:",err)
  21. }
  22. var conf Config
  23. err=json.Unmarshal(content, &conf)
  24. if err!=nil{
  25. fmt.Print("Error:",err)
  26. }
  27. json.Unmarshal(content, &conf)
  28. fmt.Println(conf.Client[0].host)
  29. }

to parse and print the first host detail from my json, that looks like this:

> {
> "Client" :
> [
> {"host":"192.168.1.2"},
> {"key":"abcdf"},
> {"secrete":"9F6w"},
> {"username":"user"},
> {"password":"password"}
> ]
> }

But I got an empty string. Could someone know the reason?

答案1

得分: 4

三个需要修复的问题:

  • json.Unmarshal要求结构体字段首字母大写才能被导出,否则它们会被忽略。
  • 你需要在字段后面添加json:"<name>"的指定符,这样解析器才知道结构体字段与JSON的映射关系。
  • 你的JSON数据创建了多个只填充了一个字段的客户端,而不是一个填充了所有字段的客户端。

参考示例:https://play.golang.org/p/oY7SppWNDC

英文:

Three things to fix:

  • json.Unmarshal requires the struct fields to be capitalized to be exported or they are ignored
  • You need the json:&quot;&lt;name&gt;&quot; specifier after the fields so the unmarshal knows the struct field to json mapping
  • Your json was making multiple clients with one field filled in instead of one client with all the fields filled in

See example: https://play.golang.org/p/oY7SppWNDC

答案2

得分: 1

这是我的问题的解决方案:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "encoding/json"
  6. )
  7. type Client struct {
  8. Host string `json:"host"`
  9. Key string `json:"apikey"`
  10. Secret string `json:"secret"`
  11. Username string `json:"username"`
  12. Password string `json:"password"`
  13. }
  14. type Config struct {
  15. Client Client `json:"Client"`
  16. }
  17. func main() {
  18. jsonmsg, err := ioutil.ReadFile("conf2.json")
  19. conf := new(Config)
  20. err = json.Unmarshal([]byte(jsonmsg), &conf)
  21. if err != nil {
  22. fmt.Print("Error:", err)
  23. }
  24. fmt.Printf("%+v\n%+v\n%+v\n%+v\n%+v\n", conf.Client.Host, conf.Client.Key, conf.Client.Secret, conf.Client.Username, conf.Client.Password)
  25. }

这是你的问题的解决方案的代码。它是一个使用Go语言编写的程序,用于读取名为"conf2.json"的配置文件,并解析其中的JSON数据。然后,它将解析后的数据打印出来,包括Client结构体中的Host、Key、Secret、Username和Password字段的值。

英文:

Here, it is the solution to my problem:
package main

  1. import (
  2. &quot;fmt&quot;
  3. &quot;io/ioutil&quot;
  4. &quot;encoding/json&quot;
  5. )
  6. type Client struct {
  7. Host string `json:&quot;host&quot;`
  8. Key string `json:&quot;apikey&quot;`
  9. Secret string `json:&quot;secret&quot;`
  10. Username string `json:&quot;username&quot;`
  11. Password string `json:&quot;password&quot;`
  12. }
  13. type Config struct {
  14. Client Client `json:&quot;Client&quot;`
  15. }
  16. func main(){
  17. jsonmsg, err := ioutil.ReadFile(&quot;conf2.json&quot;)
  18. conf := new(Config)
  19. err = json.Unmarshal([]byte(jsonmsg), &amp;conf)
  20. if err != nil {
  21. fmt.Print(&quot;Error:&quot;, err)
  22. }
  23. fmt.Printf(&quot;%+v\n%+v\n%+v\n%+v\n%+v\n&quot;, conf.Client.Host, conf.Client.Key, conf.Client.Secret, conf.Client.Username,conf.Client.Password)
  24. }

huangapple
  • 本文由 发表于 2016年2月24日 06:29:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/35589541.html
匿名

发表评论

匿名网友

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

确定