首页 > Beego config包解析嵌套 json 出现的问题

Beego config包解析嵌套 json 出现的问题

我的 json 配置文件是嵌套的,类似:

{
    // Blog global settings
    "global": {
        "title": "title",               // Blog title
        "subtitle": "subtitle"          // Blog subtitle
    },

    // Database settings. If you don't know what the value is, leave it empty.
    "database": {
        "host": "host",                 // The host name of mongoDB server, could be localhost.
        "port": "port",                 // Port
        "database": "database",         // Database name
        "username": "username",
        "password": "password"
    },

    // Admin users of the blog.
    "users": [
        {
            "username": "username",
            "email": "email",
            "password": "password"
        }
    ]
}

这样我取出database的子对象的时候采用:

temp, err := userconf.DIY("database")
if err != nil {
    panic(err)
}

 dbconf, ok := temp.(config.ConfigContainer)
 if !ok {
    panic("Configuration file error, can't read database settings.")
 }

运行的话就会进入!ok的 panic,我打印的结果,temp是一个 map,这样的话是不是就不能再用 config 包来读取dbconf的配置了?


最新版本现在支持直接jsonconf.String("database::host")获取值

if db, err := jsonconf.DIY("database"); err != nil {
        t.Fatal(err)
    } else if m, ok := db.(map[string]interface{}); !ok {
        t.Fatal("db not map[string]interface{}")
    } else {
        if m["host"].(string) != "host" {
            t.Fatal("get host err")
        }
    }
【热门文章】
【热门文章】