我想生成这个 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)
}
最佳答案
应用以下更改:
type S struct {
Statement []Statement
}
...
s_array := []Statement{*reso}
statement := &S{Statement: s_array}
希望这应该清楚:您需要一片 Statement 对象,而不仅仅是一个 Statement。
关于json - 在 Go 中表示 JSON 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4730634/