Unmarshal JSON返回空结构

问题描述 投票:1回答:2

这是我的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函数。我看了很多类似的问题,看起来我几乎和答案中的代码完全一样,但我无法让我的代码工作。

json go unmarshalling
2个回答
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
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.