教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 Go 学习笔记 09 | Golang 结构体与 JSON 互相转换

Go 学习笔记 09 | Golang 结构体与 JSON 互相转换

发布时间:2022-03-20   编辑:jiaochengji.com
教程集为您提供Go 学习笔记 09 | Golang 结构体与 JSON 互相转换等资源,欢迎您收藏本站,我们将为您提供最新的Go 学习笔记 09 | Golang 结构体与 JSON 互相转换资源
<h2>一、Golang 结构体与 JSON 互相转换</h2>

JSON 是一种轻量级的数据交换格式。RESTful API 接口中返回的数据都是 JSON 数据。

JSON 基本格式:

<pre><code class="python">{ key: value, }</code></pre>

结构体转 JSON 举例

<pre><code class="go">package main import ( "fmt" "encoding/json" ) type Student struct { ID int Gender string Name string Sno string } func main() { var s1 = Student{ ID: 12, Gender: "男", Name: "李四", Sno: "s001", } fmt.Printf("%#v\n", s1) jsonByte, _ := json.Marshal(s1) jsonStr := string(jsonByte) fmt.Printf("%v", jsonStr) }</code></pre>

输出:

<pre><code>main.Student{ID:12, Gender:"男", Name:"李四", Sno:"s001"} {"ID":12,"Gender":"男","Name":"李四","Sno":"s001"}</code></pre>

JSON 转结构体举例

<pre><code class="go">package main import ( "fmt" "encoding/json" ) type Student struct { ID int Gender string Name string Sno string } func main() { var str = `{"ID":12,"Gender":"男","Name":"李四","Sno":"s001"}` // 使用反引号 ` 可以方便在多个引号 " 下不需要使用转义符号来表示其他引号 " var s1 Student err := json.Unmarshal([]byte(str), &s1) if err != nil { fmt.Println(err) } fmt.Printf("%#v\n", s1) fmt.Println(s1.Name) }</code></pre>

输出:

<pre><code>main.Student{ID:12, Gender:"男", Name:"李四", Sno:"s001"} 李四</code></pre>

私有属性不能被 JSON 包访问

<pre><code class="go">package main import ( "fmt" "encoding/json" ) type Student struct { ID int Gender string name string // 私有属性不能被 JSON 包访问 Sno string } func main() { var s1 = Student{ ID: 12, Gender: "男", name: "李四", Sno: "s001", } fmt.Printf("%#v\n", s1) jsonByte, _ := json.Marshal(s1) jsonStr := string(jsonByte) fmt.Printf("%v", jsonStr) }</code></pre>

输出:

<pre><code>main.Student{ID:12, Gender:"男", name:"李四", Sno:"s001"} {"ID":12,"Gender":"男","Sno":"s001"}</code></pre><h2>二、结构体标签</h2><pre><code class="go">package main import ( "fmt" "encoding/json" ) type Student struct { Id int `json:"id"` // 通过 tag 指定转换成的 json 字符串的 key Gender string `json:"gender"` name string `json:"name"` Sno string `json:"sno"` } func main() { var s1 = Student{ Id: 12, Gender: "男", name: "李四", Sno: "s001", } fmt.Printf("%#v\n", s1) jsonByte, _ := json.Marshal(s1) jsonStr := string(jsonByte) fmt.Printf("%v", jsonStr) }</code></pre>

输出:

<pre><code>main.Student{Id:12, Gender:"男", name:"李四", Sno:"s001"} {"id":12,"gender":"男","sno":"s001"}</code></pre>

Golang JSON 序列化:把结构体数据转化成 JSON 格式的字符串。

Golang JSON 反序列化:把 JSON 数据转化成 Golang 中的结构体对象。

<h2>三、参考教程</h2>

Golang 教程 P34

到此这篇关于“Go 学习笔记 09 | Golang 结构体与 JSON 互相转换”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
Go 学习笔记 09 | Golang 结构体与 JSON 互相转换
【golang】结构体与json相互转换,map与json相互转换
go语言JSON处理
Golang笔记:语法,并发思想,web开发,Go微服务相关
Go Web编程--解析JSON请求和生成JSON响应
想系统学习GO语言(Golang
golang 网络编程(10)文本处理
Golang学习笔记(五):Go语言与C语言的区别
golang几种常用配置文件使用方法总结(yaml、toml、json、xml、ini)
golang学习笔记13 Golang 类型转换整理 go语言string、int、int64、float64、complex 互相转换...

[关闭]
~ ~