自定义结构体数组的JSON编码。

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

JSON encode of array of own struct

问题

我尝试读取一个目录,并将文件条目转换为JSON字符串。但是json.encoder.Encode()函数只返回空对象。为了测试,我在一个临时目录中有两个文件:

test1.js test2.js

Go程序如下:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. )
  9. type File struct {
  10. name string
  11. timeStamp int64
  12. }
  13. func main() {
  14. files := make([]File, 0, 20)
  15. filepath.Walk("/home/michael/tmp/", func(path string, f os.FileInfo, err error) error {
  16. if f == nil {
  17. return nil
  18. }
  19. name := f.Name()
  20. if len(name) > 3 {
  21. files = append(files, File{
  22. name: name,
  23. timeStamp: f.ModTime().UnixNano() / int64(time.Millisecond),
  24. })
  25. // grow array if needed
  26. if cap(files) == len(files) {
  27. newFiles := make([]File, len(files), cap(files)*2)
  28. for i := range files {
  29. newFiles[i] = files[i]
  30. }
  31. files = newFiles
  32. }
  33. }
  34. return nil
  35. })
  36. fmt.Println(files)
  37. encoder := json.NewEncoder(os.Stdout)
  38. encoder.Encode(&files)
  39. }

它产生的输出是:

  1. [{test1.js 1444549471481} {test2.js 1444549481017}]
  2. [{},{}]

为什么JSON字符串是空的?

英文:

I try to read a directory and make a JSON string out of the file entries. But the json.encoder.Encode() function returns only empty objects. For test I have two files in a tmp dir:

  1. test1.js test2.js

The go program is this:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. )
  9. type File struct {
  10. name string
  11. timeStamp int64
  12. }
  13. func main() {
  14. files := make([]File, 0, 20)
  15. filepath.Walk("/home/michael/tmp/", func(path string, f os.FileInfo, err error) error {
  16. if f == nil {
  17. return nil
  18. }
  19. name := f.Name()
  20. if len(name) > 3 {
  21. files = append(files, File{
  22. name: name,
  23. timeStamp: f.ModTime().UnixNano() / int64(time.Millisecond),
  24. })
  25. // grow array if needed
  26. if cap(files) == len(files) {
  27. newFiles := make([]File, len(files), cap(files)*2)
  28. for i := range files {
  29. newFiles[i] = files[i]
  30. }
  31. files = newFiles
  32. }
  33. }
  34. return nil
  35. })
  36. fmt.Println(files)
  37. encoder := json.NewEncoder(os.Stdout)
  38. encoder.Encode(&files)
  39. }

And the out that it produces is:

  1. [{test1.js 1444549471481} {test2.js 1444549481017}]
  2. [{},{}]

Why is the JSON string empty?

答案1

得分: 2

这是你要翻译的内容:

由于File结构体中的字段都没有导出,所以它无法工作。

以下代码可以正常工作:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. )
  9. type File struct {
  10. Name string
  11. TimeStamp int64
  12. }
  13. func main() {
  14. files := make([]File, 0, 20)
  15. filepath.Walk("/tmp/", func(path string, f os.FileInfo, err error) error {
  16. if f == nil {
  17. return nil
  18. }
  19. name := f.Name()
  20. if len(name) > 3 {
  21. files = append(files, File{
  22. Name: name,
  23. TimeStamp: f.ModTime().UnixNano() / int64(time.Millisecond),
  24. })
  25. // grow array if needed
  26. if cap(files) == len(files) {
  27. newFiles := make([]File, len(files), cap(files)*2)
  28. for i := range files {
  29. newFiles[i] = files[i]
  30. }
  31. files = newFiles
  32. }
  33. }
  34. return nil
  35. })
  36. fmt.Println(files)
  37. encoder := json.NewEncoder(os.Stdout)
  38. encoder.Encode(&files)
  39. }
英文:

It doesn't work because none of the fields in the File struct are exported.

The following works just fine:

<!-- language: lang-golang -->

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;os&quot;
  6. &quot;path/filepath&quot;
  7. &quot;time&quot;
  8. )
  9. type File struct {
  10. Name string
  11. TimeStamp int64
  12. }
  13. func main() {
  14. files := make([]File, 0, 20)
  15. filepath.Walk(&quot;/tmp/&quot;, func(path string, f os.FileInfo, err error) error {
  16. if f == nil {
  17. return nil
  18. }
  19. name := f.Name()
  20. if len(name) &gt; 3 {
  21. files = append(files, File{
  22. Name: name,
  23. TimeStamp: f.ModTime().UnixNano() / int64(time.Millisecond),
  24. })
  25. // grow array if needed
  26. if cap(files) == len(files) {
  27. newFiles := make([]File, len(files), cap(files)*2)
  28. for i := range files {
  29. newFiles[i] = files[i]
  30. }
  31. files = newFiles
  32. }
  33. }
  34. return nil
  35. })
  36. fmt.Println(files)
  37. encoder := json.NewEncoder(os.Stdout)
  38. encoder.Encode(&amp;files)
  39. }

huangapple
  • 本文由 发表于 2015年10月11日 15:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/33062750.html
匿名

发表评论

匿名网友

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

确定