2016-07-12 10 views
0

GoでJSONファイルの解析に問題があります。GoでJSONファイルを解析する

エラーは発生していませんが、出力が得られません。

私はいくつかの異なる方法を試しましたが、私は動作するようには見えません。 ご協力いただければ幸いです。前もって感謝します。

package simplefiles 

import (
    "encoding/json" 
    "fmt" 
    "os" 
) 

//PluginInfo - struct for plugins.json 
var PluginInfo struct { 
    LatestVersion string `json:"latest_version"` 
    LastUpdated  string `json:"last_updated"` 
    Popular   bool  `json:"popular"` 
    Info   []string `json:"Info"` 
} 

//ParsePlugins - parses plugins.json 
func ParsePlugins() { 
    pluginsFile, err := os.Open("test.json") 
    if err != nil { 
     fmt.Println("opening config file", err.Error()) 
    } 
    jsonParser := json.NewDecoder(pluginsFile) 
    if err = jsonParser.Decode(&PluginInfo); err != nil { 
     fmt.Println("parsing config file", err.Error()) 
    } else { 
     fmt.Printf(PluginInfo.LastUpdated) 
    } 
    return 
} 

JSONサンプル:

{ 
    "my-test-site":{ 
     "latest_version":"6.4.5", 
     "last_updated":"2016-05-22T00:23:00.000Z", 
     "popular":true, 
     "infomation":[ 
     { 
      "id":6043, 
      "title":"Test info 1", 
      "created_at":"2014-08-01T10:58:35.000Z", 
      "updated_at":"2015-05-15T13:47:24.000Z", 
      "published_date":null, 
      "references":{ 
       "url":[ 
        "http://samplesite1.com", 
        "http://samplesite2.com" 
       ] 
      }, 
      "site_type":"info", 
      "fixed_v":"1.10" 
     } 
     ] 
    }, 
    "another-test-site":{ 
     "latest_version":"2.1.0", 
     "last_updated":"2016-06-12T08:36:00.000Z", 
     "popular":false, 
     "infomation":[ 
     { 
      "id":6044, 
      "title":"Test site 2 info", 
      "created_at":"2014-08-01T10:58:35.000Z", 
      "updated_at":"2015-05-15T13:47:24.000Z", 
      "published_date":null, 
      "references":{ 
       "otherinfo":[ 
        "blah blah blah" 
       ] 
      }, 
      "site_type":"search", 
      "fixed_v":"1.2.0" 
     } 
     ] 
    } 
} 
+0

定義 –

+1

あなたの構造体は、文字列の配列として、「情報」を指定し、「動作しない」を、あなたの実際のJSONはハッシュの配列として "情報"を持っています。これはどうしたの? –

答えて

4

あなたの問題は、あなたのJSONデータをそのまま使用すると、定義された構造体の型に文字列のマップではなく、構造体の型であることです。あなたは少しそれが動作以下のようにコードを修正していますが、各構造体の値を取得するためのマップへのインデックスが必要な場合:

package main 

import (
     "encoding/json" 
     "fmt" 
     "os" 
) 

//PluginInfo - struct for plugins.json 
var PluginInfo map[string]struct { // NOTICE map of string to struct 
     LatestVersion string `json:"latest_version"` 
     LastUpdated string `json:"last_updated"` 
     Popular  bool  `json:"popular"` 
     Info   []string `json:"Info"` 
} 

//ParsePlugins - parses plugins.json 
func ParsePlugins() { 
     pluginsFile, err := os.Open("test.json") 
     if err != nil { 
       fmt.Println("opening config file", err.Error()) 
     } 
     jsonParser := json.NewDecoder(pluginsFile) 
     if err = jsonParser.Decode(&PluginInfo); err != nil { 
       fmt.Println("parsing config file", err.Error()) 
     } else { 
       for key, val := range PluginInfo { 
         fmt.Printf("Key %q, last updated %s\n", key, val.LastUpdated) 
       } 
     } 
     return 
} 

func main() { 
     ParsePlugins() 
}