Unable to parse a complex json in golang

2019-04-28 03:30发布

I want to parse this JSON (in config/synch.conf):

{
    "period" :"yy",
    "exec_period" :  
        {
            "start" : {
                "month" : 1,
                "week" : 2,
                "day" : 3,
                "hour" : 4,
                "minute" : 5
            },
            "end" : {
                "month" : 6,
                "week" : 7,
                "day" : 8,
                "hour" : 9,
                "minute" : 10
            }
        },
    "backup" : [
        {
            "local_dir" : "directoryLo1",
            "server_dir" :  "directoryLo2",
            "server_host" : "domaineName"
        },
        {
            "local_dir" : "directoryLo1",
            "server_dir" :  "directorySe2",
            "server_host" : "domaineName"
        }
    ],
    "incremental_save" : "1Y2M"
}

With this programm:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
)

func main() {
    content, err := ioutil.ReadFile("config/synch.conf")
    if err == nil {

        type date struct{
            month float64
            week float64
            day float64
            hour float64
            minute float64
        }

        type period struct{
            start date
            end date
        }

        type backupType []struct{
            local_dir string
            server_dir string
            server_host string
        }

        type jason struct{
            period string
            exec_period period
            backup backupType
            incremental_save string
        }

        var parsedMap jason

        err := json.Unmarshal(content, &parsedMap)

        if err!= nil {
            fmt.Println(err)
        }

        fmt.Println(parsedMap)
    } else {
        panic(err)
    }

}

Which doesn't work as expected, as the output is:

{ {{0 0 0 0 0} {0 0 0 0 0}} [] }

Here is the same example at play.golang.org
http://play.golang.org/p/XoMJIDIV59

I don't know if this is possible with go, but I wanted to get the value of the json.Unmarshal function stored in a map[string]interface{} (or another object that allows that) where I could access, for example, the value of the minute's end (10) like this: parsedMap["exec_period"]["end"]["minute"], but I don't uderstand the "Generic JSON withinterface{}" part of JSON and Go at golang.org

标签: json parsing go
1条回答
看我几分像从前
2楼-- · 2019-04-28 03:57

Your code is fine except that the json package can only work with exported fields.

Everything will work if you capitalize the first letter for each field name:

type date struct {
    Month  float64
    Week   float64
    Day    float64
    Hour   float64
    Minute float64
}

type period struct {
    Start date
    End   date
}

type backupType []struct {
    Local_dir   string
    Server_dir  string
    Server_host string
}

type jason struct {
    Period           string
    Exec_period      period
    Backup           backupType
    Incremental_save string
}

While it is possible to marshal into a map[string]interface{}, if the data has a set structure (such as the one in your question), your solution is most likely preferable. Using interface{} would require type assertions and might end up looking messy. Your example would look like this:

parsedMap["exec_period"].(map[string]interface{})["end"].(map[string]interface{})["minute"].(float64)
查看更多
登录 后发表回答