This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Encoding/json", "FMT") type change struct {Mid int//Menu idactions []string//Owned permission ' Add ' "View" "Delete" "Update"}type change_slice struct {Chgarr []change//a role-corresponding menu and Permissions}func Main () {//object is serialized as a JSON string-------------- -------------------Beginvar C1, C2 changevar msg change_slicec1. Mid = 1c1. Actions = []string{"View", "Add"}c2. Mid = 2c2. Actions = []string{"Delete", "Add", "Update"}msg. Chgarr = []change{c1, c2}fmt. PRINTLN (msg) b, er: = json. Marshal (msg) if er = = nil {fmt. The Println (string (b))}//object is serialized as a JSON string---------------------------------end// JSON string deserialized to object---------------------------------beginvar str string = ' {' Chgarr ': [{' Mid ': 1, ' Actions ': [' View ', ' add '] },{"Mid": 2, "Actions": ["Delete", "Add", "Update"]}]} ' var msgs change_sliceerr: = json. Unmarshal ([]byte (str), &MSGS) if err! = Nil {fmt. Println ("Can ' t decode JSON message", err)} else {fmt. Println (msgs. CHGARR[1]. Mid)}//json string deserialized to object---------------------------------End}
Output Result:
{[{1 [view add]} {2 [delete add UPDATE]}]}
{"Chgarr": [{"Mid": 1, "Actions": ["View", "Add"]},{"Mid": 2, "Actions": ["Delete", "Add", "Update"]}]}
2