Unmarshal JSON返回空结构

时间:2017-12-07 17:35:46

标签: json go unmarshalling

这是我的JSON文件:

{
    "database": {
        "dialect": "mysql"
        "host": "localhost",
        "user": "root",
        "pass": "",
        "name": "sws"
    }
}

这是我的代码:

package config

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

type ConfigType struct {
    Database DatabaseType `json:"database"`
}

type DatabaseType struct {
    Dialect string `json:"dialect"`
    Host string `json:"host"`
    User string `json:"user"`
    Pass string `json:"pass"`
    Name string `json:"name"`
}

func Config() {
    file, err := os.Open("./config/config.json")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    fileBytes, _ := ioutil.ReadAll(file)

    var Conf ConfigType
    json.Unmarshal(fileBytes, &Conf)

    fmt.Printf("File content:\n%v", string(fileBytes))
    fmt.Printf("Conf: %v\n", Conf)
    fmt.Printf("Content: \n %v \nType: %T", Conf.Database.Host, Conf)
}

这是输出:

File content:
{
    "database": {
        "dialect": "mysql"
        "host": "localhost",
        "user": "root",
        "pass": "",
        "name": "sws"
    }
}
Conf: {{    }}
Content: 

Type: config.ConfigType%

将包导入main,只执行Config函数。我看了很多类似的问题,似乎我几乎和答案中的代码完全相同,但是我无法让代码工作。

2 个答案:

答案 0 :(得分:4)

除非你想知道你的应用程序无法正常工作,否则错误不会慷慨地归还给你。不要忽略错误! ioutil.ReadAll()会返回错误。 json.Unmarshal()会返回错误。检查那些!

如果您添加了错误检查,json.Unmarshal()会返回:

panic: invalid character '"' after object key:value pair

Go Playground上尝试此操作。

您的输入JSON无效。您在"dialect"行中缺少逗号。添加缺少的逗号(在Go Playground上尝试):

Conf: {{mysql localhost root  sws}}
Content: 
 localhost 
Type: main.ConfigType

答案 1 :(得分:1)

don't neglect the errors ,always keep track of errors if function is returning one . It helps you find out if somethings goes wrong.  

> In your case json file is invalid you missed "," after "dialect": "mysql" .
> valid json file should be (config.json )

     {
       "database": {
          "dialect": "mysql",
          "host": "localhost",
          "user": "root",
          "pass": "",
          "name": "sws"
       }
     }

> 
> 
> Modified code 

    package main
    import (
       "encoding/json"
       "fmt"
       "io/ioutil"
       "log"
       "os"
    )

    type ConfigType struct {
       Database DatabaseType `json:"database"`
    }

    type DatabaseType struct {
       Dialect string `json:"dialect"`
       Host    string `json:"host"`
       User    string `json:"user"`
       Pass    string `json:"pass"`
       Name    string `json:"name"`
    }

    func main() {
       file, err := os.Open("./config.json")
       if err != nil {
           log.Fatal(err)
       }
       defer file.Close()

       fileBytes, err := ioutil.ReadAll(file)
       if err != nil {
           fmt.Println("error reading file", err)
           return
       }

      var Conf ConfigType
      err = json.Unmarshal(fileBytes, &Conf)

      if err != nil {
          fmt.Println("unmarshalling error ", err)
          return
      }
      fmt.Printf("File content:\n%v\n", string(fileBytes))
      fmt.Printf("Conf: %v\n", Conf)
      fmt.Printf("Content: \n %v \nType: %T", Conf.Database.Host, Conf)
}

> Output 

    File content:
    {
      "database": {
      "dialect": "mysql",
      "host": "localhost",
      "user": "root",
      "pass": "",
      "name": "sws"
      }
    }
    Conf: {{mysql localhost root  sws}}
    Content: 
    localhost 
    Type: main.ConfigType