2011-01-19 44 views
0

我想生成这个JSON政策:代表围棋JSON政策

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

我展示以下解决方案将产生以下JSON:

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

如何改变这种做法, “声明”:有一个数组值?

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) 
} 

回答

2

采用以下修改:

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

希望这应该说清楚:你要Statement对象的一个​​切片,而不仅仅是一个单一的声明。