我正在使用Polymer Google Charts。我想爲一個Controller.ServeJSON調用創建JSON,但如果它是更好的設計,則需要兩個。Golang to Json,創建Google Charts
這是一個JSON包含的例子,如果是兩次調用。 ' rows ='[{「label」:「Mth」,「type」 [ 「揚」,31],[ 「月」,28],[ 「月」,31]] '
而如果它是一個
數據='[[ 「月」, 「日」 ],[ 「揚」,31],[ 「月」,28],[ 「月」,31]]」
這可以在https://elements.polymer-project.org/elements/google-chart
發現我用https://mholt.github.io/json-to-go/到JSON轉換成結構。
假設兩個電話,得到這個..
type Chart struct {
Cols []struct {
Label string `json:"label"`
Type string `json:"type"`
} `json:"cols"`
Rows []struct {
Num0 string `json:"0"`
Num1 int `json:"1"`
所有幫助和建議將最讚賞。
乾杯。
編輯..
func (c *ChillController) Get() {
jsonString := "[[\"Month\", \"Days\"], [\"Jan\", 31], [\"Feb\", 28], [\"Mar\", 31]]"
d := [][]Chart{}
json.Unmarshal([]byte(jsonString), &d)
fmt.Printf("%v", &d)
c.Data["json"] = &d
c.ServeJSON()
}
而且這回..
[ [ 空, 空 ], [ 空, 空 ] ]
我錯過了什麼。
感謝。我似乎並沒有完全掌握這個概念。我試圖實現這一變化。請參閱上面的修改。我錯過了什麼。 –
您正試圖將一個切片解組爲一個Chart結構,一個小技巧:如果您在json中看到「{}」,請使用地圖結構,並使用[]切片。我編輯了我的答案:) –