Marshal返回了我的结构体的空JSON。

huangapple go评论73阅读模式
英文:

Marshal returns empty json of my struct

问题

我正在编写一段代码,用于将一个目录扫描到一个结构体中,然后将其导出为JSON格式。

目前,我的代码使用ScanDir函数成功地扫描了一个目录,但是当我尝试将我的结构体编组为JSON时,它只返回{}

// 文件结构体
type Fic struct {
	nom     string    `json:"fileName"`
	lon     int64     `json:"size"`
	tim     time.Time `json:"lastFileUpdate"`
	md5hash []byte    `json:"md5"`
}

// 文件夹结构体
type Fol struct {
	subFol []Fol     `json:"listFolders"`
	files  []Fic     `json:"listFiles"`
	nom    string    `json:"folderName"`
	tim    time.Time `json:"lastFolderUpdate"`
}

func main() {
	var root Fol
	err := ScanDir("./folder", &root) // 扫描一个文件夹并填充我的结构体
	check(err)
	b, err := json.Marshal(root)
	check(err)
	os.Stdout.Write(b)
}

func check(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "致命错误:%s", err.Error())
		os.Exit(1)
	}
}

请注意,结构体字段的标签应该使用双引号而不是HTML实体编码。

英文:

I'm working on a code that scan a repertory into a struct in order to export it to json.

Currently, my code scans finely a repertory with the ScanDir function, but when I try to Marshal my struct it only returns {}.

// file's struct
type Fic struct {
	nom     string    `json:"fileName"`
	lon     int64     `json:"size"`
	tim     time.Time `json:"lastFileUpdate"`
	md5hash []byte    `json:"md5"`
}

// folder's struct
type Fol struct {
	subFol []Fol     `json:"listFolders"`
	files  []Fic     `json:"listFiles"`
	nom    string    `json:"folderName"`
	tim    time.Time `json:"lastFolderUpdate"`
}

func main() {
	var root Fol
	err := ScanDir("./folder", &root)	// scan a folder and fill my struct
	check(err)
	b, err := json.Marshal(root)
	check(err)
	os.Stdout.Write(b)
}

func check(err error) {
if err != nil {
	fmt.Fprintf(os.Stderr, "Fatal error : %s", err.Error())
	os.Exit(1)
}

答案1

得分: 6

为了进行JSON的编组和解组,结构体的字段/属性需要是公共的。要使结构体的字段/属性公共,它们应该以大写字母开头。在你的代码中,所有的字段都是小写的。

type Fic struct {
    Nom     string    `json:"fileName"`
    Lon     int64     `json:"size"`
    Tim     time.Time `json:"lastFileUpdate"`
    Md5hash []byte    `json:"md5"`
}

// 文件夹的结构体
type Fol struct {
    SubFol []Fol     `json:"listFolders"`
    Files  []Fic     `json:"listFiles"`
    Nom    string    `json:"folderName"`
    Tim    time.Time `json:"lastFolderUpdate"`
}

以上是翻译好的内容。

英文:

In order to marshal and unmarshal json, fields/property of struct needs to be public. To make the field/property of struct public it should start with Upper Case. In your all the fields are in lower case.

type Fic struct {
    Nom     string    `json:"fileName"`
    Lon     int64     `json:"size"`
    Tim     time.Time `json:"lastFileUpdate"`
    Md5hash []byte    `json:"md5"`
}

// folder's struct
type Fol struct {
    SubFol []Fol     `json:"listFolders"`
    Files  []Fic     `json:"listFiles"`
    Nom    string    `json:"folderName"`
    Tim    time.Time `json:"lastFolderUpdate"`
}

huangapple
  • 本文由 发表于 2015年12月6日 00:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/34108129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定