如何处理Go中具有相同元素的嵌套结构体

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

How to deal with a nested struct with the same elements in Go

问题

我一直在为我的一个小项目编写这段代码,我想解析一些看起来像这样的JSON数据:

{
    "payloads": [
        {
            "xss": [
                {
                    "payload": "{{RANDSTR}}\"><scRiPT>alert(1)</ScrIPT>{{RANDSTR}}",
                    "search": "{{RANDSTR}}\"><scRi"
                },
                {
                    "payload": "{{RANDSTR}}\"",
                    "search": "{{RANDSTR}}\""
                },
                {
                    "payload": "{{RANDSTR}}'",
                    "search": "{{RANDSTR}}'"
                }
            ],
            "tpli": [
                {
                    "payload": "{{RANDSTR}}${{ {{RANDINT}} * {{RANDINT}} }}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{{ {{RANDINT}} * {{RANDINT}} }}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{! {{RANDINT}} * {{RANDINT}} !}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{% {{RANDINT}} * {{RANDINT}} %}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                }
            ]
        }
    ]
}

这是我的结构声明:

type Payload struct {
	Payload []struct {
		Payload string `json:"payload"`
		Search  string `json:"search"`
	}
}

type Payloads struct {
	Payloads []Payload `json:"payloads"`
}

我知道这不是我应该这样做的方式,但我不确定最好的方法。我不想指定键(xss,tpli等),因为我希望能够轻松扩展这个文件,而不必修改Go文件。

有人可以指点我如何实现这个吗?

提前感谢。

英文:

I have been writing this code for a small project of mine and I want to parse some JSON data which looks like this:

{
    "payloads": [
        {
            "xss": [
                {
                    "payload": "{{RANDSTR}}\"><scRiPT>alert(1)</ScrIPT>{{RANDSTR}}",
                    "search": "{{RANDSTR}}\"><scRi"
                },
                {
                    "payload": "{{RANDSTR}}\"",
                    "search": "{{RANDSTR}}\""
                },
                {
                    "payload": "{{RANDSTR}}'",
                    "search": "{{RANDSTR}}'"
                }
            ],
            "tpli": [
                {
                    "payload": "{{RANDSTR}}${{ {{RANDINT}} * {{RANDINT}} }}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{{ {{RANDINT}} * {{RANDINT}} }}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{! {{RANDINT}} * {{RANDINT}} !}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{% {{RANDINT}} * {{RANDINT}} %}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                }
            ]
        }
    ]
}

This is my struct declaration:


type Payload struct {
Payload []struct {
Payload string `json:"payload"`
Search  string `json:"search"`
}
}
type Payloads struct {
Payloads []Payload `json:"payloads"`
}

I know this isn't how I'm supposed to do it but I'm not sure the best way. I don't want to specify the keys (xss, tpli, etc) as I want to easily expand this file without having to modify the Go file.

Can someone point me in the right direction on how to achieve this?

Thanks in advance.

答案1

得分: 5

以下是翻译好的内容:

type Payloads struct {
	Payloads []map[string][]Payload `json:"payloads"`
}

type Payload struct {
	Payload string `json:"payload"`
	Search  string `json:"search"`
}

Playground: https://play.golang.org/p/S6nnOKkADUO

英文:

Model it as such:

type Payloads struct {
Payloads []map[string][]Payload `json:"payloads"`
}
type Payload struct {
Payload string `json:"payload"`
Search  string `json:"search"`
}

Playground: https://play.golang.org/p/S6nnOKkADUO

huangapple
  • 本文由 发表于 2021年10月8日 05:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/69488210.html
匿名

发表评论

匿名网友

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

确定