Golang将JSON对象传递给函数

huangapple go评论86阅读模式
英文:

Golang pass JSON object to a function

问题

这是关于Selenium Web driver的内容,但我认为它并不是非常重要。

我可以设置浏览器名称:

caps := selenium.Capabilities{"browserName": "firefox"}
wd, _ := selenium.NewRemote(caps, "")

但是对于"proxy",例如:

caps := selenium.Capabilities{"proxy": "http://1.2.3.4:999"}
wd, _ := selenium.NewRemote(caps, "")

我必须传递一个JSON代理对象,但我完全不知道如何创建它...我在这里和那里搜索,但仍然无法弄清楚...它是一种结构体吗?还是映射..或者其他什么... Golang将JSON对象传递给函数

英文:

This is regarded the Selenium Web driver but I think it is not quite important.

I can set the browser name

caps := selenium.Capabilities{"browserName": "firefox"}
wd, _ := selenium.NewRemote(caps, "")

But for "proxy" ie:

caps := selenium.Capabilities{"proxy": "http://1.2.3.4:999"}
wd, _ := selenium.NewRemote(caps, "")

I have to pass a JSON Proxy Object which I absolutely have no idea how to create... I searched there and there, but still could not figure... Is it kind of struct? Or map.. or what... Golang将JSON对象传递给函数

答案1

得分: 2

如我在评论中所说,你可以使用以下形式:

selenium.Capabilities{
    "proxy": map[string]interface{}{
        "httpProxy": "http://1.2.3.4:999",
        // 其他属性
    }
}

非结构化的 JSON 通常通过 map[string]interface{} 进行 (un)marshalling,而 selenium.Capabilities 类型实际上就是一个 map[string]interface{}

参考资料:JSON 和 Go

英文:

As I've said in the comment, you can use the form

selenium.Capabilities{
    "proxy": map[string]interface{}{
        "httpProxy": "http://1.2.3.4:999",
        // etc.
    }
}

Unstructured JSON is usually (un)marshalled through map[string]interface{}, and the type selenium.Capabilities is in fact just a map[string]interface{}.

See also: JSON and Go.

huangapple
  • 本文由 发表于 2015年5月7日 19:38:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/30099970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定