6

この JSON を解析したい (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"
}

このプログラムでは:

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

}

出力が次のようになるため、これは期待どおりに機能しません。

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

play.golang.org
http://play.golang.org/p/XoMJIDIV59の同じ例を次に示します。

これがgoで可能かどうかはわかりませんが、json.Unmarshalたとえば値分終わり (10) のようparsedMap["exec_period"]["end"]["minute"]に:

4

1 に答える 1