This is a creation in Article, where the information may have evolved or changed.
Go to generate JSON and JSON data is very simple, through the official "Encoding/json" package, called JSON. Marshal () generates data and calls JSON. Unmarshal () or JSON. Newdecoder.decode () parses the data.
Package Mainimport ("Encoding/json" "FMT" "IO" "OS" "strings") type Order struct {Name string ' json:name ' OrderItem []it Em ' Json:item ' orderrefund []refund ' json:refund '}type item struct {Name string ' json:name ' Oid int ' json:oid '}type Refund struct {Name string ' json:name ' Item int ' json:item ' Content string ' json:content ' Img s string ' Json:imgs ' Status string ' json:status '}//json to struct body func jsontostruct () {Const Jsonstream = ' {' Id ': one, ' Na Me ":" A "} {" id ":" name ":" B "} {" id ":" C "} {" id ":" {"}," name ":" D "} {" id ":" "Name": "E"} ' dec: = json. Newdecoder (Strings. Newreader (Jsonstream)) for {var m messageerr: = Dec. Decode (&m) If err = = Io. EOF {Break} else if err! = Nil {fmt. PRINTLN (Err)} else {fmt. Printf ("%d:%s\n", M.id, M.name)}}}//struct to Jsonfunc Structtojson () {var m Order m.name = "10010" M.orderitem = Append (M.orderitem, Item{ono: "Shanghai_hourse", oid:1}) M.orderitem = Append (M.orderitem, Item{ono: "Beijing_hourse", OiD:2}) for I: = 1; I < 6; i++ {str: = []byte ("things") str = StrConv. Appendint (str, int64 (i), ten) Orderi: = item{ono:string (str), oid:i} M.orderitem = Append (M.orderitem, Orderi )} bytes, _: = json. Marshal (m) fmt. Printf ("json:m,%s\n", bytes)}func main () {jsontostruct () Structtojson ()}