将mapstructure的JSON映射为具有嵌套字典的结构。

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

mapstructure json into a structure with nested dictionary

问题

我是你的中文翻译助手,以下是翻译的内容:

我是golang的新手。我有一个带有嵌套结构的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",但似乎也失败了。

我感谢任何对此的帮助。

英文:

I am new to golang. I have a json file with nested structures that I want to parse and populate.

I am trying to use mapstructure to try and populate. I am able to do it for simple structures. But when it comes to array for dictionaries(key:struct). The map[string]interface{} seems to fail with runtime error: index out of range.

I tried to do the following for the json example below.

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
}

Since the window id is random I am not able to define it in a struct.
I tried using mapstructure:",squash" but that seems to fail as well.

I appreciate any help with this.

答案1

得分: 1

@Burak Serdar是正确的。

>你不需要使用mapstructure。JSON解组可以处理这个。

你的代码有很多错误,比如结构体、大写字母、'views'等等。

以下是一个示例:

package main

import (
	"encoding/json"
	"fmt"
)

var data = `
{
    "desktop":{
        "view":[
            {
                "random_id1_1":{
                    "loc":[
                        0,
                        0,
                        640,
                        360
                    ],
                    "wrtc":"some string",
                    "label":"window 1"
                },
                "random_id1_2":{
                    "loc":[
                        640,
                        360,
                        1280,
                        720
                    ],
                    "wrtc":"some string blah",
                    "label":"window 2"
                }
            },
            {
                "random_id2_1":{
                    "loc":[
                        0,
                        0,
                        640,
                        360
                    ],
                    "wrtc":"some string",
                    "label":"window 1"
                },
                "random_id2_2":{
                    "loc":[
                        640,
                        360,
                        1280,
                        720
                    ],
                    "wrtc":"some string blah",
                    "label":"window 2"
                }
            }
        ],
        "rotationSpeed":30
    }
}
`

type Window struct {
	Loc   []int
	Wrtc  string
	Label string
}

type Desktop struct {
	View           []map[string]Window
	Rotation_speed int `json:"rotationSpeed"`
}

type Config struct {
	Desktop Desktop
}

func main() {
	c := Config{}
	json.Unmarshal([]byte(data), &c)
	fmt.Println("json.Unmarshal: ", c)
}

输出结果为:

json.Unmarshal:  {{[map[random_id1_1:{[0 0 640 360] some string window 1} random_id1_2:{[640 360 1280 720] some string blah window 2}] map[random_id2_1:{[0 0 640 360] some string window 1} random_id2_2:{[640 360 1280 720] some string blah window 2}]] 30}}

如果你想要使用mapstructure来处理View结构体,你也可以使用"remain"标签:

type Window struct {
	Loc   []int
	Wrtc  string
	Label string
}

type View struct {
	Windows map[string]Window `mapstructure:",remain"`
}

type Desktop struct {
	View           []View
	Rotation_speed int `json:"rotationSpeed"`
}

type Config struct {
	Desktop Desktop
}

func main() {
	c2 := Config{}
	m := map[string]interface{}{}
	_ = json.Unmarshal([]byte(data), &m)
	mapstructure.Decode(m, &c2)
	fmt.Println("mapstructure: ", c2)
}
英文:

@Burak Serdar is right

>You don't need mapstructure. JSON unmarshaling can deal with this.

you code have many error, like struct, uppercase, 'views' etc..

follow is a demo:

package main
import (
"encoding/json"
"fmt"
)
var data = `
{
"desktop":{
"view":[
{
"random_id1_1":{
"loc":[
0,
0,
640,
360
],
"wrtc":"some string",
"label":"window 1"
},
"random_id1_2":{
"loc":[
640,
360,
1280,
720
],
"wrtc":"some string blah",
"label":"window 2"
}
},
{
"random_id2_1":{
"loc":[
0,
0,
640,
360
],
"wrtc":"some string",
"label":"window 1"
},
"random_id2_2":{
"loc":[
640,
360,
1280,
720
],
"wrtc":"some string blah",
"label":"window 2"
}
}
],
"rotationSpeed":30
}
}
`
type Window struct {
Loc   []int
Wrtc  string
Label string
}
type Desktop struct {
View           []map[string]Window
Rotation_speed int `json:"rotationSpeed" mapstructure:"rotationSpeed"`
}
type Config struct {
Desktop Desktop
}
func main() {
c := Config{}
json.Unmarshal([]byte(data), &c)
fmt.Println("json.Unmarshal: ", c)
}
json.Unmarshal:  {{[map[random_id1_1:{[0 0 640 360] some string window 1} random_id1_2:{[640 360 1280 720] some s
tring blah window 2}] map[random_id2_1:{[0 0 640 360] some string window 1} random_id2_2:{[640 360 1280 720] some
string blah window 2}]] 30}}

also you can use mapstructure by "remain", if you want View struct

type Window struct {
Loc   []int
Wrtc  string
Label string
}
type View struct {
Windows map[string]Window `mapstructure:",remain"`
}
type Desktop struct {
View           []View
Rotation_speed int `json:"rotationSpeed" mapstructure:"rotationSpeed"`
}
type Config struct {
Desktop Desktop
}
func main() {
c2 := Config{}
m := map[string]interface{}{}
_ = json.Unmarshal([]byte(data), &m)
mapstructure.Decode(m, &c2)
fmt.Println("mapstructure: ", c2)
}

huangapple
  • 本文由 发表于 2023年1月11日 07:29:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75076908.html
匿名

发表评论

匿名网友

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

确定