在 Go 中表示 JSON 策略

发布于 2024-10-12 09:32:58 字数 1031 浏览 5 评论 0原文

我想生成此 JSON 策略:

{"Statement":[{"Resource":"RESOURCE","Condition":{"DateLessThan":{"AWS:EpochTime":EXPIRES}}}]}

下面显示的解决方案会生成以下 JSON:

{"Statement":{"Resource":"example.com","Condition":{"DateLessThan":{"AWS:EpochTime":"1234543"}}}}

如何更改此设置以使“Statement”:具有数组值?

package main 
import ( 
        "json" 
        "fmt" 
) 

type S struct { 
        Statement Statement 
} 

type Statement struct { 
        Resource  string 
        Condition Date 
} 

type Date struct { 
        DateLessThan AWS 
} 

type AWS struct { 
        EpochTime string "AWS:EpochTime" 
} 

func main() { 
        expires := "1234543" 
        resource := "example.com" 
        date := &AWS{EpochTime: expires} 
        date2 := &Date{DateLessThan:*date} 
        reso := &Statement{Resource: resource, Condition: *date2} 
        statement := &S{Statement: *reso} 
        result1, _ := json.Marshal(statement) 
        fmt.Printf(result1) 
} 

I would like to Generate this JSON policy:

{"Statement":[{"Resource":"RESOURCE","Condition":{"DateLessThan":{"AWS:EpochTime":EXPIRES}}}]}

The solution I'm showing below produces the following JSON:

{"Statement":{"Resource":"example.com","Condition":{"DateLessThan":{"AWS:EpochTime":"1234543"}}}}

How do I change this so that "Statement": has an array value?

package main 
import ( 
        "json" 
        "fmt" 
) 

type S struct { 
        Statement Statement 
} 

type Statement struct { 
        Resource  string 
        Condition Date 
} 

type Date struct { 
        DateLessThan AWS 
} 

type AWS struct { 
        EpochTime string "AWS:EpochTime" 
} 

func main() { 
        expires := "1234543" 
        resource := "example.com" 
        date := &AWS{EpochTime: expires} 
        date2 := &Date{DateLessThan:*date} 
        reso := &Statement{Resource: resource, Condition: *date2} 
        statement := &S{Statement: *reso} 
        result1, _ := json.Marshal(statement) 
        fmt.Printf(result1) 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2024-10-19 09:32:58

应用以下更改:

type S struct { 
    Statement []Statement 
}
...
    s_array := []Statement{*reso}
    statement := &S{Statement: s_array}

希望这应该清楚地表明:您需要一个 Statement 对象的切片,而不仅仅是一个单独的 Statement。

Apply the following changes:

type S struct { 
    Statement []Statement 
}
...
    s_array := []Statement{*reso}
    statement := &S{Statement: s_array}

Hopefully that should make it clear: you want a slice of Statement objects, rather than just a single Statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文