2011-01-19 42 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對象的一個​​切片,而不僅僅是一個單一的聲明。