go json序列

go json序列

// test2 project main.go
package main

import (
	"encoding/json"
	"fmt"
	//	"github.com/golang/protobuf/proto"
)

type DanWei struct {
	Unitid   string
	Unitname string
}

type DanWeis struct {
	UnitsArr []*DanWei
}

func main() {
	var dw DanWei
	dw.Unitid = "1"
	dw.Unitname = "个"
	var dw2 DanWei
	dw2.Unitid = "2"
	dw2.Unitname = "双"
	var dws DanWeis
	dws.UnitsArr = append(dws.UnitsArr, &dw)
	dws.UnitsArr = append(dws.UnitsArr, &dw2)
	b, _ := json.Marshal(dws)
	fmt.Println(string(b)) //{"UnitsArr":[{"Unitid":"1","Unitname":"个"},{"Unitid":"2","Unitname":"双"}]}
	//b2, _ := proto.Marshal(&dws)
	//fmt.Println(string(b2))
}

  

posted @ 2022-07-21 11:42  delphi中间件  阅读(37)  评论(0编辑  收藏  举报