{
"devices": [
{
"id": 20081691,
"targetIp": "10.1.1.1",
"iops": "0.25 IOPS per GB",
"capacity": 20,
"allowedVirtualGuests": [
{
"Name": "akhil1"
},
{
"Name": "akhil2"
}
]
}
]
}
如何編寫此JSON數據的結構表示,以便我可以添加和刪除設備到列表中。我嘗試了不同的結構表示,但沒有任何工作。下面是我用類似的json數據嘗試過的例子之一。我無法向其添加新數據。該結構的代表性和追加做可能是錯在這裏golang中以下json數據的結構表示是什麼?
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
ID string `json:"id,omitempty"`
Firstname string `json:"firstname,omitempty"`
Lastname string `json:"lastname,omitempty"`
Address []Address `json:"address,omitempty"`
}
type Address[] struct {
City string `json:"city,omitempty"`
}
func main() {
var people []Person
people = append(people, Person{ID: "1", Firstname: "Nic", Lastname: "Raboy", Address: []Address{{City: "Dublin"},{ City: "CA"}}})
b, err := json.Marshal(people)
if err != nil {
fmt.Println("json err:", err)
}
fmt.Println(string(b))
}
非常感謝。我們可以通過將AllowedVirtualGuests字段定義爲[] struct而不是AllowedVirtualGuests [] VirtualGuest –
應該能夠做到這一點。請參閱現有答案:http://stackoverflow.com/questions/32531854/how-to-initialize-nested-structure-array-in-golang – keno