dongzhuo1958
dongzhuo1958
2017-07-01 10:46

在Go中使用未知键值从JSON创建字符串映射

  • dictionary
  • string
  • json

I try to create a map of strings from a JSON with an undefined number of unknow key-values.

Here is my example JSON file:

{
         "localhost":
        {
                "tag": "dev_latest",
                "vhost": "localhost.com"
        },
        "development":
        {
                "tag": "dev_latest",
                "vhost": "dev.com"
        }
}

I want to create a map[string]string with value like this:

config := map[string]string{
    "localhost-tag":      "dev_latest",
    "localhost-vhost": "localhost.com,
    "development-tag":   "dev_latest,
    ...
}

To parse a JSON with "github.com/jmoiron/jsonq" with known values, is quite easy, but in this case, localhost can be anything and tag can be any other thing.

My entry point in my Go code is like this:

func ParseJson(){
    configPath := GetConfigPath()
    b, err := ioutil.ReadFile(configPath) 

     //  Here, I need to create my map of strings..

    return configKeyStr

}

Any help will be really appreciate.

Thanks!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐