doudou6050 2016-07-12 04:06
浏览 40
已采纳

在Go中解析JSON文件

I am having trouble parsing a JSON file in Go.

I am not getting any errors, but I am not getting an output.

I have tried a few different methods, but I can't seem to get any to work. Any help would be greatly appreciated. Thanks in advance.

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 Sample:

{  
   "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"
         }
      ]
   }
}
  • 写回答

1条回答 默认 最新

  • douguaidian8021 2016-07-12 04:21
    关注

    Your problem is that your JSON data is a map of string to the struct type you defined, not the struct type directly. If you modify your code slightly as below it works, but you need to index into the map to get each struct value:

    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
    ", key, val.LastUpdated)
                    }
            }
            return
    }
    
    func main() {
            ParsePlugins()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (有偿)在ANSYS中 .anf文件
  • ¥45 关于#芯片#的问题:组合逻辑电路设计
  • ¥15 基与机器学习和时间序列分析预测养老服务需求趋势
  • ¥100 求连续两帧图像在水平和垂直上偏移
  • ¥15 mysql全文索引查找指定必须关键词word无效
  • ¥15 Verilog hdl密码锁设计
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题