0
A
回答
0
下面是一个示例程序,它使用encoding/json
软件包对一个简单结构进行序列化和反序列化。请参阅代码注释以获得解释。
请注意,我在这里省略了错误处理。
package main
import (
"bytes"
"encoding/json"
"fmt"
)
// your data structure need not be exported, i.e. can have lower case name
// all exported fields will by default be serialized
type person struct {
Name string
Age int
}
func main() {
writePerson := person{
Name: "John",
Age: 32,
}
// encode the person as JSON and write it to a bytes buffer (as io.Writer)
var buffer bytes.Buffer
_ = json.NewEncoder(&buffer).Encode(writePerson)
// this is the JSON-encoded text
// output: {"Name":"John","Age":32}
fmt.Println(string(buffer.Bytes()))
// deocde the person, reading from the buffer (as io.Reader)
var readPerson person
_ = json.NewDecoder(&buffer).Decode(&readPerson)
// output: {John 32}
fmt.Println(readPerson)
}
相关问题
- 1. 与地图[字符串]字符串数据编Golang yaml.v2结构失败
- 2. Java树字符串数据结构
- 3. 字符串索引的数据结构?
- 4. 字符串集合的数据结构
- 5. 编组包含字符串的结构
- 6. Golang - 结构
- 7. 如何转换Golang中包含其他结构的结构?
- 8. C#字符串去结构
- 9. 结构字符串问题
- 10. Golang结构比较
- 11. Golang代码结构
- 12. golang一个结构
- 13. Golang和JSON结构
- 14. Golang混淆结构
- 15. 与结构与golang
- 16. Highcharts以x数据,y值golang结构
- 17. 输入字符串的不结构
- 18. Golang:将json字符串解码为来自mysql的结构数据库db
- 19. Golang结构数组不工作
- 20. 指针在结构常数字符串
- 21. 字符串数组去结构
- 22. 字符串的结构数组
- 23. PHP字符串数组结构
- 24. 字符串数组结构阵列
- 25. 字符的结构和字符串
- 26. Golang将Json字符串转换为结构体
- 27. 将连接字符串解析为golang中的结构
- 28. 串口数据结构
- 29. 包含结构的结构与包含指针的结构
- 30. 如何过滤包含字符串和数组的结构?
序列化零值肯定会是最简单的解决方案。 – JimB
可能是其中一个包https://golang.org/pkg/encoding/#pkg-subdirectories – jcbwlkr