如何在Go中将字节/uint8数组编组为JSON数组?

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

How to marshal a byte/uint8 array as json array in Go?

问题

我有一个包含[]uint8成员的结构体,并且我正在使用json.Marshal将其写入。问题是,它将uint8解释为char,并输出一个字符串而不是一个数字数组。

如果是[]int,我可以使其工作,但是如果可以避免的话,我不想分配和复制项目。我能做到吗?

英文:

I've got a struct with a []uint8 member and I'm writing it with json.Marshal. Trouble is, it's interpreting the uint8s as chars and it outputs a string rather than an array of numbers.

I can get this to work if it's a []int, but I don't want to have to allocate and copy over the items if I can avoid it. Can I?

答案1

得分: 28

根据文档[]byte将被编码为Base64字符串。

“数组和切片值被编码为JSON数组,除了[]byte被编码为Base64编码的字符串,nil切片被编码为null JSON对象。”

所以我认为你可能需要让你的结构体实现Marshaler接口,通过实现自己的MarshalJSON方法,将[]uint8编码为更理想的JSON数组。

以这个例子为例:

  1. import "fmt"
  2. import "encoding/json"
  3. import "strings"
  4. type Test struct {
  5. Name string
  6. Array []uint8
  7. }
  8. func (t *Test) MarshalJSON() ([]byte, error) {
  9. var array string
  10. if t.Array == nil {
  11. array = "null"
  12. } else {
  13. array = strings.Join(strings.Fields(fmt.Sprintf("%d", t.Array)), ",")
  14. }
  15. jsonResult := fmt.Sprintf(`{"Name":%q,"Array":%s}`, t.Name, array)
  16. return []byte(jsonResult), nil
  17. }
  18. func main() {
  19. t := &Test{"Go", []uint8{'h', 'e', 'l', 'l', 'o'}}
  20. m, err := json.Marshal(t)
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. fmt.Printf("%s", m) // {"Name":"Go","Array":[104,101,108,108,111]}
  25. }

http://play.golang.org/p/Tip59Z9gqs


或者也许更好的想法是创建一个新类型,将[]uint8作为其底层类型,将该类型设为Marshaler,并在你的结构体中使用该类型。

  1. import "fmt"
  2. import "encoding/json"
  3. import "strings"
  4. type JSONableSlice []uint8
  5. func (u JSONableSlice) MarshalJSON() ([]byte, error) {
  6. var result string
  7. if u == nil {
  8. result = "null"
  9. } else {
  10. result = strings.Join(strings.Fields(fmt.Sprintf("%d", u)), ",")
  11. }
  12. return []byte(result), nil
  13. }
  14. type Test struct {
  15. Name string
  16. Array JSONableSlice
  17. }
  18. func main() {
  19. t := &Test{"Go", []uint8{'h', 'e', 'l', 'l', 'o'}}
  20. m, err := json.Marshal(t)
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. fmt.Printf("%s", m) // {"Name":"Go","Array":[104,101,108,108,111]}
  25. }

http://play.golang.org/p/6aURXw8P5d

英文:

According to the docs, a []byte will be encoded as a Base64 string.

>"Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON object."

So I think that you may need to make your struct implement the Marshaler interface by implementing your own MarshalJSON method that makes a more desirable JSON array encoding out of your []uint8.

Take this example:

  1. import "fmt"
  2. import "encoding/json"
  3. import "strings"
  4. type Test struct {
  5. Name string
  6. Array []uint8
  7. }
  8. func (t *Test) MarshalJSON() ([]byte, error) {
  9. var array string
  10. if t.Array == nil {
  11. array = "null"
  12. } else {
  13. array = strings.Join(strings.Fields(fmt.Sprintf("%d", t.Array)), ",")
  14. }
  15. jsonResult := fmt.Sprintf(`{"Name":%q,"Array":%s}`, t.Name, array)
  16. return []byte(jsonResult), nil
  17. }
  18. func main() {
  19. t := &Test{"Go", []uint8{'h', 'e', 'l', 'l', 'o'}}
  20. m, err := json.Marshal(t)
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. fmt.Printf("%s", m) // {"Name":"Go","Array":[104,101,108,108,111]}
  25. }

http://play.golang.org/p/Tip59Z9gqs


Or maybe a better idea would be to make a new type that has []uint8 as its underlying type, make that type a Marshaler, and use that type in your struct.

  1. import "fmt"
  2. import "encoding/json"
  3. import "strings"
  4. type JSONableSlice []uint8
  5. func (u JSONableSlice) MarshalJSON() ([]byte, error) {
  6. var result string
  7. if u == nil {
  8. result = "null"
  9. } else {
  10. result = strings.Join(strings.Fields(fmt.Sprintf("%d", u)), ",")
  11. }
  12. return []byte(result), nil
  13. }
  14. type Test struct {
  15. Name string
  16. Array JSONableSlice
  17. }
  18. func main() {
  19. t := &Test{"Go", []uint8{'h', 'e', 'l', 'l', 'o'}}
  20. m, err := json.Marshal(t)
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. fmt.Printf("%s", m) // {"Name":"Go","Array":[104,101,108,108,111]}
  25. }

http://play.golang.org/p/6aURXw8P5d

huangapple
  • 本文由 发表于 2013年1月6日 08:10:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/14177862.html
匿名

发表评论

匿名网友

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

确定