您好,登录后才能下订单哦!
本篇内容主要讲解“如何使用Go语言JSON 标准库”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Go语言JSON 标准库”吧!
序列化使用 json 库中的Marshal函数:
func Marshal(v interface{}) ([]byte, error)比如使用以下的结构体表示一部电影:
	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"]}
一个字典要想序列化成- 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 的名称。
直接看一个例子:
	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我们要先将 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]]
无法确定的格式可以直接使用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 标准库”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。