我们如何在Golang中的结构体中初始化一个存储JSON输出的结构体类型数组?

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

How can we initialize an array of type struct (which stores the json output) within a struct in golang

问题

我需要初始化以下数据结构,它将存储一个JSON。Attack_plans将保存多个计划,如果我遍历GeneratePlan结构,我需要获取存储的所有计划。

  1. type GeneratePlan struct {
  2. Mode string `json:"mode"`
  3. Name string `json:"name"`
  4. Schema string `json:"schema"`
  5. Version string `json:"version"`
  6. Attack_plans []struct {
  7. Attack_plan *Attack_plan `json:"attack-plan"`
  8. } `json:"attack-plans"`
  9. }
  10. type Attack_plan struct {
  11. Attack_resources []struct {
  12. Attack_resource *Attack_resource `json:"attack-resource"`
  13. } `json:"attack-resources"`
  14. }

有人可以提供建议吗?如果在初始化之前需要简化数据结构,请也提出建议。我对golang非常陌生,请忽略最佳实践。感谢任何帮助!

英文:

I need to initialize the following data structure which will store a json. The Attack_plans will hold multiple plans and if I loop through the GeneratePlan struct, I need all the plans that were stored.

  1. type GeneratePlan struct {
  2. Mode string `json:"mode"`
  3. Name string `json:"name"`
  4. Schema string `json:"schema"`
  5. Version string `json:"version"`
  6. Attack_plans []struct {
  7. Attack_plan *Attack_plan `json:"attack-plan"`
  8. } `json:"attack-plans"`
  9. }
  10. type Attack_plan struct {
  11. Attack_resources []struct {
  12. Attack_resource *Attack_resource `json:"attack-resource"`
  13. } `json:"attack-resources"`
  14. }

Can anyone please suggest something? If the data structure needs to be simplified before initializing it, then please suggest that as well. I am very new to golang so please ignore the best practices to follow. Any help is appreciated. Thanks!

答案1

得分: 0

为什么不直接使用json.marshal将对象转换为JSON字符串,你就可以得到答案了。

英文:

why don't u just json.marshal your object to a json string, you can got answer

答案2

得分: 0

生成计划 := GeneratePlan{
Mode: "模式",
Name: "名称",
Schema: "模式",
Version: "版本",
Attack_plans: []struct {
Attack_plan *Attack_plan json:"攻击计划"
}{
{Attack_plan: &Attack_plan{[]struct {
Attack_resource *Attack_resource json:"攻击资源"
}{
{Attack_resource: new(Attack_resource)},
{Attack_resource: new(Attack_resource)},
}}},
{Attack_plan: &Attack_plan{[]struct {
Attack_resource *Attack_resource json:"攻击资源"
}{
{Attack_resource: new(Attack_resource)},
{Attack_resource: new(Attack_resource)},
}}},
},
}

英文:
  1. generatePlan := GeneratePlan{
  2. Mode: "mode",
  3. Name: "name",
  4. Schema: "sachema",
  5. Version: "version",
  6. Attack_plans: []struct {
  7. Attack_plan *Attack_plan `json:"attack-plan"`
  8. }{
  9. {Attack_plan: &Attack_plan{[]struct {
  10. Attack_resource *Attack_resource `json:"attack-resource"`
  11. }{
  12. {Attack_resource: new(Attack_resource)},
  13. {Attack_resource: new(Attack_resource)},
  14. }}},
  15. {Attack_plan: &Attack_plan{[]struct {
  16. Attack_resource *Attack_resource `json:"attack-resource"`
  17. }{
  18. {Attack_resource: new(Attack_resource)},
  19. {Attack_resource: new(Attack_resource)},
  20. }}},
  21. },
  22. }

答案3

得分: 0

我找到了解决方案!这简化了上述的数据结构!

  1. type GeneratePlan struct{
  2. Mode string `json:"mode"`
  3. Name string `json:"name"`
  4. Schema string `json:"schema"`
  5. Version string `json:"version"`
  6. Attack_plans []struct1 `json:"attack-plans"`
  7. }
  8. type struct1 struct {
  9. Attack_plan Attack_plan `json:"attack-plan"`
  10. }
  11. type Attack_plan struct{
  12. Attack_resouces []struct2 `json:"attack-resources"`
  13. }
  14. type struct2 struct {
  15. Attack_resource Attack_resource `json:"attack-resource"`
  16. }

以上是翻译好的代码部分。

英文:

I found the solution! This simplifies the above data structure!

  1. type GeneratePlan struct{
  2. Mode string `json:"mode"`
  3. Name string `json:"name"`
  4. Schema string `json:"schema"`
  5. Version string `json:"version"`
  6. Attack_plans []struct1 `json:"attack-plans"`
  7. }
  8. type struct1 struct {
  9. Attack_plan Attack_plan `json:"attack-plan"`
  10. }
  11. type Attack_plan struct{
  12. Attack_resouces []struct2 `json:"attack-resources"`
  13. }
  14. type struct2 struct {
  15. Attack_resource Attack_resource `json:"attack-resource"`
  16. }

huangapple
  • 本文由 发表于 2016年12月1日 09:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/40901352.html
匿名

发表评论

匿名网友

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

确定