json - 在 Go 中表示 JSON 策略

标签 json amazon-web-services marshalling go

我想生成这个 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/

相关文章:

python - 如何在 Amazon Linux AMI 2018.03.0 (HVM) 上正确设置 cron 作业?

json - 无法使用Spark读取json数据

mysql 不返回正确的 json

amazon-web-services - 如何更改 AWS Elastic Beanstalk 中的应用程序名称

amazon-web-services - 从 ARN 生成到 AWS 管理控制台的链接

java - JAXB 解码不起作用

c# - 编码指向数组 P/Invoke 的指针

json - 从编码结构 Golang 中删除转义字符

java - 将 JSON 对象转换为 Map 对象

javascript - 使用 javascript 更改 JSON 结构