英文:
Marshalling multidimensional array in Go
问题
以下是要翻译的内容:
代码如下,但简化版本是我有一个包含uint8
类型的多维数组(切片)的结构体。我尝试创建一个方法,可以将任何结构体转储到文件中,但内部数组的情况...嗯,说实话我不太确定。以下是一个更大的代码库的简洁版本,但可以说明问题。我对Go相对较新,所以可能是我对类型转换的某些事情不知情。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type MyStruct struct {
MyArray [][]uint8 `json:"arr"`
Name string `json:"name"`
}
func CreateMyStruct(name string, size uint16, fill uint8) *MyStruct {
var m [][]uint8
var i, j uint16
for i = 0; i < size; i++ {
sl := []uint8{}
for j = 0; j < size; j++ {
sl = append(sl, fill)
}
m = append(m, sl)
}
return &MyStruct{
MyArray: m,
Name: name,
}
}
func main() {
myStruct := CreateMyStruct("bobby", 4, 1)
fmt.Printf("%+v\n", myStruct)
str, _ := json.Marshal(myStruct)
fmt.Printf("%+v\n", str)
ioutil.WriteFile("my.json", str, 0644)
}
输出结果为:
$ go run test.go
&{MyArray:[[1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1]] Name:bobby}
[123 34 97 114 114 34 58 91 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 93 44 34 110 97 109 101 34 58 34 98 111 98 98 121 34 125]
$ cat my.json
{"arr":["AQEBAQ==","AQEBAQ==","AQEBAQ==","AQEBAQ=="],"name":"bobby"}
显然,我期望的是:
[[1,1,1,1]] // 等等
英文:
Code below, but the short version is I have a multidimensional array (slice) of uint8
in a struct. I tried to create a method that I could dump any struct into and it would write to a file, but the inner arrays are...well I'm not sure to be honest. The following is a succinct version of a larger code base but gets the point across. I am relatively new to Go so maybe its something about type conversions I'm just unaware of.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type MyStruct struct {
MyArray [][]uint8 `json:"arr"`
Name string `json:"name"`
}
func CreateMyStruct(name string, size uint16, fill uint8) *MyStruct {
var m [][]uint8
var i, j uint16
for i = 0; i < size; i++ {
sl := []uint8{}
for j = 0; j < size; j++ {
sl = append(sl, fill)
}
m = append(m, sl)
}
return &MyStruct{
MyArray: m,
Name: name,
}
}
func main() {
myStruct := CreateMyStruct("bobby", 4, 1)
fmt.Printf("%+v\n", myStruct)
str, _ := json.Marshal(myStruct)
fmt.Printf("%+v\n", str)
ioutil.WriteFile("my.json", str, 0644)
}
The output being:
$ go run test.go
&{MyArray:[[1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1]] Name:bobby}
[123 34 97 114 114 34 58 91 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 93 44 34 110 97 109 101 34 58 34 98 111 98 98 121 34 125]
$ cat my.json
{"arr":["AQEBAQ==","AQEBAQ==","AQEBAQ==","AQEBAQ=="],"name":"bobby"}
Obviously, I'm expecting something like:
[[1,1,1,1]] // etc
答案1
得分: 2
在Golang中,[]byte
是[]uint8
的别名,而json.Marshall
将[]byte
编码为base64编码的字符串(正如你所看到的):
https://pkg.go.dev/encoding/json#Marshal
我怀疑解决方案是编写自己的JSON编码器以生成你想要的JSON。
示例中包含了一个自定义编码器的文档:
https://pkg.go.dev/encoding/json#example-package-CustomMarshalJSON
这里是一个示例:https://play.golang.org/p/N2K8QfmPNnc
由于是在手机上费力地输入,所以可能有些不完美,还请谅解。
英文:
In Golang []byte
is an alias for []uint8
and json.Marshall
encodes []byte
as base64-encoded strings (as you're seeing):
https://pkg.go.dev/encoding/json#Marshal
I suspect the solution is to write your own JSON Marshaller to generate JSON as you want.
An example of a custom Marshaller is included in the docs for the package:
https://pkg.go.dev/encoding/json#example-package-CustomMarshalJSON
Here: https://play.golang.org/p/N2K8QfmPNnc
Typed tediously on my phone, so apologies it's hacky
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论