2017-12-07 5 views
1

Hier ist meine JSON-Datei:Abstellungs JSON zurückgibt leer struct

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

Hier ist mein Code:

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) 
} 

Und hier ist die Ausgabe:

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

Type: config.ConfigType% 

Das Paket main importiert und nur die Config Funktion wird ausgeführt. Ich habe mir viele ähnliche Fragen angeschaut und es scheint, als hätte ich fast den gleichen Code wie in den Antworten, aber ich kann meinen Code nicht zum Laufen bringen.

Antwort

4

Fehler werden Ihnen nicht gnädig zurückgewiesen, es sei denn, Sie möchten keine Ahnung haben, warum Ihre App nicht funktioniert. Fehler nicht auslassen! ioutil.ReadAll() gibt einen Fehler zurück. json.Unmarshal() gibt einen Fehler zurück. Überprüfen Sie diese!

Sollten Sie Fehlerprüfung hinzugefügt, json.Unmarshal() kehrt:

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

Versuchen Sie dies auf der Go Playground.

Ihr Eingabe-JSON ist ungültig. Sie haben ein fehlendes Komma in der Zeile "dialect". Hinzufügen des fehlenden Komma (versuchen Sie es auf dem Go Playground):

Conf: {{mysql localhost root sws}} 
Content: 
localhost 
Type: main.ConfigType 
0
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 
Verwandte Themen