【发布时间】:2023-01-11 12:14:51
【问题描述】:
我是戈朗的新手。我有一个 json 文件,其中包含我想要解析和填充的嵌套结构。
我正在尝试使用 mapstructure 来尝试填充。我能够为简单的结构做到这一点。但是当涉及到字典数组(键:结构)时。 map[string]interface{} 似乎因 runtime error: index out of range 而失败。
我尝试对下面的 json 示例执行以下操作。
type Window struct {
loc []int
wrtc string
label string
}
type View struct {
windows []Window
}
type Views struct {
views []View
}
type Desktop struct {
views []Views `mapstructure:views`
rotation_speed string `mapstructure:"rotationSpeed" json:rotationSpeed"`
}
func main() {
file, _ := ioutil.ReadFile("test.json")
data := Desktop{}
_ = json.Unmarshal([]byte(file), &data)
fmt.Println("data: ", data.views[0])
}
{
"desktop": {
"view": [{// configs for view1
"random_id1": {
"loc": [0,0,640,360],
"wrtc": "some string",
"label": "window 1"
},
"random_id213443": {
"loc": [640,360,1280,720],
"wrtc": "some string blah",
"label": "window 2"
},
// more windows with random ids....
},
{
// configs for view2...
}
],
"rotationSpeed": 30
}
由于窗口 id 是随机的,我无法在结构中定义它。
我尝试使用mapstructure:",squash",但似乎也失败了。
感谢您对此提供的任何帮助。
【问题讨论】:
-
你不需要地图结构。 JSON 解组可以解决这个问题。您需要导出您的结构成员(将它们大写)。
view元素是一个[]map[string]View,其中 View 是每个视图的结构。 -
您也可以通过更改建模来避免(而不是解决)此问题,而不是键中的随机 ID,它们可以在值中,因此从映射更改为数组。
-
这回答了你的问题了吗? JSON and dealing with unexported fields
-
@BurakSerdar 非常感谢您的回复。我导出了struct的成员,把View成员改成了View Structure的map。现在它没有段错误,但似乎 View 数组中没有任何内容(所以基本上它没有填充它。我还尝试更改 json 结构以使
random_id成为 @Cadmium 建议的结构的一部分,但我无法得到它解析。 -
发布更新的代码,我们可以尝试找出
标签: go