2017-12-07 16 views
1

を返します。非整列化JSONは、ここに私のJSONファイルだ空のstruct

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

ここに私のコードです:

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

Type: config.ConfigType% 

パッケージがmainにインポートされます。

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

そして、ここでは、出力されますConfig関数だけが実行されます。私は似たような質問をたくさん見てきましたが、答えとまったく同じコードをほとんど持っているようですが、私のコードを動作させることはできません。

答えて

4

あなたのアプリがうまくいかない理由がわからない場合を除き、間違ってエラーを返すことはありません。エラーを省略しないでください! ioutil.ReadAll()がエラーを返します。 json.Unmarshal()はエラーを返します。それらをチェックしてください!

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

Go Playgroundでこれを試してみてください:

あなたはエラーチェック、json.Unmarshal()リターンを追加すべき。

あなたの入力JSONが無効です。 "dialect"行にカンマがありません。

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 
:行方不明カンマ( Go Playground上でそれを試してみてください)を追加します
関連する問題