Go 语言 JSON 标准库的使用
Go 语言中的 encoding/json 库提供了复杂的将 Go 中各种类型与JSON格式之间转换的功能, 我们主要使用以下几个功能:
- 将一个切片、结构体或字典序列化成 JSON 格式的字符串【字节流】。
- 将一个 JSON 格式的字符串【字节流】反序列化成一个切片、结构体或字典。
序列化
序列化使用 json 库中的Marshal函数:
func Marshal(v interface{}) ([]byte, error) 
1. 结构体序列化
比如使用以下的结构体表示一部电影:
	type Movie struct {
		Title  string
		Year   int  `json:"released"`
		Color  bool `json:"color,omitempty"`
		Actors []string
	}
 
定义里类型后面跟的字符串 json:"released"和json:"color,omitempty,称为 field tags,它告诉 json 库在执行序列化时的一些规则:
- json:"released" 使得在序列化后对应的名字为"released",而不是"Year"。
- json:"color,omitempty"使得如果 Color 成员的值为false,那么就忽略它,不对它进行序列化。
没有 field tags 时进行序列化的一些规则:
- 如果结构体成员的名字不是以大写字母开头,则不对它进行序列化。
- 如果结构体成员的名字是以大写字母开头,则序列化后的名字就是成员名。
进行序列化的代码如下:
movie := Movie{
		Title:  "Casablanca",
		Year:   1942,
		Color:  false,
		Actors: []string{"Humphrey Bogart", "Ingrid Bergman"},
	}
	
	data, err := json.Marshal(movie)
	if err != nil {
		log.Fatalf("JSON marshaling failed: %s", err)
	}
	fmt.Printf("%s\n", data) 
输出:
{"Title":"Casablanca","released":1942,"Actors":["Humphrey Bogart","Ingrid Bergman"]}
2. 字典序列化
一个字典要想序列化成- JSON 格式,它的 key 必须是字符串。
以下是一个例子:
	info := map[string]int{
		"width":  1280,
		"height": 720,
	}
	
	data, err := json.MarshalIndent(info, "", " ")
	if err != nil {
		log.Fatalf("JSON marshaling failed: %s", err)
	}
	fmt.Printf("%s\n", data)
 
输出:
{
"height": 720,
"width": 1280
}
这里我们使用MarshalIndent函数,使得输出后的 JSON 格式更易阅读。序列化后的名字就是字典中 key 的名称。
3. 切片序列化
直接看一个例子:
	type Movie struct {
		Title  string
		Year   int  `json:"released"`
		Color  bool `json:"color,omitempty"`
		Actors []string
	}
	var movies = []Movie{
		{
			Title:  "Casablanca",
			Year:   1942,
			Color:  false,
			Actors: []string{"Humphrey Bogart", "Ingrid Bergman"},
		},
		{
			Title:  "Cool Hand Luke",
			Year:   1967,
			Color:  true,
			Actors: []string{"Paul Newman"},
		},
		{
			Title:  "Bullitt",
			Year:   1968,
			Color:  true,
			Actors: []string{"Steve McQueen", "Jacqueline Bisset"},
		},
	}
	data, err := json.MarshalIndent(movies, "", " ")
	if err != nil {
		log.Fatalf("JSON marshaling failed: %s", err)
	}
	fmt.Printf("%s\n", data)
 
输出:
[
{
"Title": "Casablanca",
"released": 1942,
"Actors": [
"Humphrey Bogart",
"Ingrid Bergman"
]
},
{
"Title": "Cool Hand Luke",
"released": 1967,
"color": true,
"Actors": [
"Paul Newman"
]
},
{
"Title": "Bullitt",
"released": 1968,
"color": true,
"Actors": [
"Steve McQueen",
"Jacqueline Bisset"
]
}
]
反序列化
反序列化使用Unmarshal函数:
func Unmarshal(data []byte, v interface{}) error
 
1. 明确知道 JSON 格式
我们要先将 JSON 格式表示为一个确定的类型。
1.如下的 JSON 格式:
	{
		"name": "Awesome 4K",
		"resolutions": [
			{
				"width": 1280,
				"height": 720
			},
			{
				"width": 1920,
				"height": 1080
			},
			{
				"width": 3840,
				"height": 2160
			}
		]
	}
 
我们可以用如下结构体来表示它:
struct {
	Name        string
	Resolutions []struct {
		Width  int
		Height int
	}
}
 
2.如下的 JSON 格式:
{
 "height": 720,
 "width": 1280
}
 
也可以使用map[string]int,也就是字典来表示。
3.如下的 JSON 格式:
 [
	{
		"width": 1280,
		"height": 720
	},
	{
		"width": 1920,
		"height": 1080
	},
	{
		"width": 3840,
		"height": 2160
	}
]
 
使用切片[]map[string]int来表示。
不管怎样,一个确定的JSON格式总是可以使用切片、结构体或字典来表示。
之后就可以执行反序列化了:
	var jsonBlob = []byte(`
		[
			{
				"width": 1280,
				"height": 720
			},
			{
				"width": 1920,
				"height": 1080,
			},
			{
				"width": 3840,
				"height": 2160
			}
		]
	`)
	di := []map[string]int{}
	err = json.Unmarshal(jsonBlob, &di)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v\n", di)
 
输出
[map[height:720 width:1280] map[height:1080 width:1920] map[height:2160 width:3840]]
2. 无法确定 JSON 格式
无法确定的格式可以直接使用interface{}类型来表示。这个时候,如果是JSON对象,则反序列化时 json 库会使用map[string]interface{}类型来表示它。如果是JSON数组,则会使用[]interface{}类型来表示它。具体interface{}对应的具体类型,就需要使用类型断言了。
具体看一个示例:
package main
import (
	"encoding/json"
	"fmt"
)
func main() {
	var jsonBlob = []byte(`
		{
			"name": "Awesome 4K",
			"price": 1999.9,
			"resolutions": [
				{
					"width": 1280,
					"height": 720
				},
				{
					"width": 1920,
					"height": 1080
				},
				{
					"width": 3840,
					"height": 2160
				}
			]
		}
	`)
	var d interface{}
	err := json.Unmarshal(jsonBlob, &d)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Println(d)
	m := d.(map[string]interface{})
	for k, v := range m {
		switch vv := v.(type) {
		case string:
			fmt.Println(k, "is string", vv)
		case float64:
			fmt.Println(k, "is float64", vv)
		case []interface{}:
			fmt.Println(k, "is an array:")
			for i, u := range vv {
				fmt.Println(i, u)
			}
		default:
			fmt.Println(k, "is of a type I don't know how to handle")
		}
	}
}
 
输出:
map[name:Awesome 4K price:1999.9 resolutions:[map[height:720 width:1280] map[height:1080 width:1920] map[height:2160 width:3840]]]
resolutions is an array:
0 map[height:720 width:1280]
1 map[height:1080 width:1920]
2 map[height:2160 width:3840]
name is string Awesome 4K
price is float64 1999.9
到此这篇关于Go 语言 JSON 标准库的使用的文章就介绍到这了,更多相关Go语言 语言 JSON 标准库内容请搜索M135模板网以前的文章或继续浏览下面的相关文章希望大家以后多多支持M135模板网!
