英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论