在Go中解析JSON文件

在Go中解析JSON文件,json,go,Json,Go,在Go中解析JSON文件时遇到问题 我没有得到任何错误,但我没有得到输出 我试过几种不同的方法,但似乎都不管用。 任何帮助都将不胜感激。提前谢谢 package simplefiles import ( "encoding/json" "fmt" "os" ) //PluginInfo - struct for plugins.json var PluginInfo struct { LatestVersion string `json:"latest

在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"
         }
      ]
   }
}

您的问题是,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()
}

您的问题是,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()
}
定义“不工作”您的结构将“info”指定为字符串数组,但实际json将“information”指定为哈希数组。这是怎么回事?定义“不起作用”您的结构将“信息”指定为字符串数组,但实际的json将“信息”指定为哈希数组。这是怎么回事?