dqol6556 2018-08-14 12:11
浏览 97

如何从字符串表示形式的变量中获取嵌套结构?

I have a JSON file looks like following somehow

{
    "type": "Weekly",
    "clients": [
        "gozo",
        "dva"
    ],
    "sender": "no-reply@flowace.in",
    "recipients": {
        "gozo": [
            "a@gmail.com",
            "b@hotmail.com"
        ],
        "dva": [
            "c@gmail.com",
            "d@hotmail.com"
        ]
    },
    "features": [
        "Top5UsedApps",
        "TimeSpentOnEachL3",
        "NewlyAssignedL3",
        "HoursLogged"
    ],
    "dbCloning": [
        "dva"
    ] 
}

I have mapped structs like the following.

type receivers struct {
    Gozo []string `json:"gozo"`
    Dva  []string `json:"dva"`
    // Add more recievers
}

// Config -- The config object parsed from the JSON file
type Config struct {
    ReportType string    `json:"type"`
    Clients    []string  `json:"clients"`
    Sender     string    `json:"sender"`
    Recipients receivers `json:"recipients"`
    Cloning    []string  `json:"dbCloning"`
}

Then somewhere in the another source file, I do the following,

func main() {
    conf := LoadConfig(os.Args[1])
    for _, client := range conf.Clients {

        // Use the client variable of some other function calls

        fmt.Println(conf.Recipients[client]) // This will not work
}

Now my question is how can I make it work. I can not loop over conf.Recipients directly.

PS: Consider that LoadConfig function unmarshal the JSON and returns a conf object.

Edit 1: It looks like it was design decision error. Now going with the solution of map[string][]string. But not marking it as an answer because the need to know how it is easily done for all the cases where there was no other choices.

  • 写回答

2条回答 默认 最新

  • duanpanhuo0618 2018-08-14 12:15
    关注

    The problem is that your type receivers shouldn't have named fields. It should be a map[string][]string instead.

    Here is a working example:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Config struct {
        ReportType string              `json:"type"`
        Clients    []string            `json:"clients"`
        Sender     string              `json:"sender"`
        Recipients map[string][]string `json:"recipients"`
        Cloning    []string            `json:"dbCloning"`
    }
    
    var data = []byte(`{
        "type": "Weekly",
        "clients": [
            "gozo",
            "dva"
        ],
        "sender": "no-reply@flowace.in",
        "recipients": {
            "gozo": [
                "a@gmail.com",
                "b@hotmail.com"
            ],
            "dva": [
                "c@gmail.com",
                "d@hotmail.com"
            ]
        },
        "features": [
            "Top5UsedApps",
            "TimeSpentOnEachL3",
            "NewlyAssignedL3",
            "HoursLogged"
        ],
        "dbCloning": [
            "dva"
        ] 
    }`)
    
    func main() {
        var conf Config
        json.Unmarshal(data, &conf)
    
        for _, client := range conf.Clients {
            fmt.Println(conf.Recipients[client])
        }
    }
    

    Gives the output

    [a@gmail.com b@hotmail.com]
    [c@gmail.com d@hotmail.com]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c