golang JSON数据格式 XML数据格式 Gob(这玩意真的有人用吗?)

接着摸鱼,摸鱼一时爽,一直摸鱼一直爽,接着搞golang
不是所有的数据都可以编码为 JSON 类型,只有验证通过的数据结构才能被编码:

  1. JSON 对象只支持字符串类型的 key;
  2. 要编码一个 Go map 类型,map 必须是 map[string]T(T 是 json 包中支持的任何类型)
  3. Channel,复杂类型和函数类型不能被编码
  4. 不支持循环数据结构;它将引起序列化进入一个无限循环
go 复制代码
// 由于golang有个非常恶心的设定,大写才可以被外部包引用,因此我们在json的时候,需要自己定义tag
// 把这个设定的人殴打一百遍的心都有了
type Address struct {
	Type    string `json:"type"`
	City    string `json:"city"`
	Country string `json:"country"`
}

type VCard struct {
	FirstName string     `json:"firstName"`
	LastName  string     `json:"lastName"`
	Addresses []*Address `json:"addresses"`
	Remark    string     `json:"remark"`
}

func main() {
	// 声明 && 赋值俩结构体 pa wa是俩指针
	pa := &Address{"private", "Aartselaar", "Belgium"}
	wa := &Address{"work", "Boom", "Belgium"}
	vc := VCard{"Jan", "Kersschot", []*Address{pa, wa}, "none"}
	// fmt.Printf("%v: \n", vc) // {Jan Kersschot [0x126d2b80 0x126d2be0] none}:
	// JSON format:这一步将结构体转成了json串
	js, _ := json.Marshal(vc)
	fmt.Printf("JSON format: %s", js)
	// using an encoder:写的文件里面
	file, _ := os.OpenFile("vcard.json", os.O_CREATE|os.O_WRONLY, 0666)
	defer file.Close()
	enc := json.NewEncoder(file)
	err := enc.Encode(vc)
	if err != nil {
		log.Println("Error in encoding json")
	}
}

json反序列化,使用json.Unmarshal()源码里面有俩参数,数据和目标

编码和解码流

json 包提供 DecoderEncoder 类型来支持常用 JSON 数据流读写。NewDecoder()
NewEncoder() 函数分别封装了 io.Reader 和 io.Writer 接口。

go 复制代码
func NewDecoder(r io.Reader) *Decoder
func NewEncoder(w io.Writer) *Encoder

要想把 JSON 直接写入文件,可以使用 json.NewEncoder 初始化文件(或者任何实现 io.Writer 的类型),并调用 Encode();反过来与其对应的是使用 json.NewDecoderDecode() 函数:

go 复制代码
func NewDecoder(r io.Reader) *Decoder
func (dec *Decoder) Decode(v interface{}) error

XML 数据格式

go 复制代码
// xml.go
package main

import (
	"encoding/xml"
	"fmt"
	"strings"
)

var t, token xml.Token
var err error

func main() {
	input := "<Person><FirstName>Laura</FirstName><LastName>Lynn</LastName></Person>"
	inputReader := strings.NewReader(input)
	p := xml.NewDecoder(inputReader)
	// 遍历xml数据格式
	for t, err = p.Token(); err == nil; t, err = p.Token() {
		switch token := t.(type) {
		// 开始节点的名字
		case xml.StartElement:
			name := token.Name.Local
			fmt.Printf("Token name: %s\n", name)
			for _, attr := range token.Attr {
				attrName := attr.Name.Local
				attrValue := attr.Value
				fmt.Printf("An attribute is: %s %s\n", attrName, attrValue)
				// ...
			}
		// 节点没了
		case xml.EndElement:
			fmt.Println("End of token")
		// 节点数据
		case xml.CharData:
			content := string([]byte(token))
			fmt.Printf("This is the content: %v\n", content)
			// ...
		default:
			// ...
		}
	}
}

使用golang内置的Gob

go 复制代码
// gob1.go
package main

import (
	"bytes"
	"fmt"
	"encoding/gob"
	"log"
)

type P struct {
	X, Y, Z int
	Name    string
}

type Q struct {
	X, Y *int32
	Name string
}

func main() {
	// Initialize the encoder and decoder.  Normally enc and dec would be      
	// bound to network connections and the encoder and decoder would      
	// run in different processes.      
	var network bytes.Buffer   // Stand-in for a network connection
	// 写二进制里面      
	enc := gob.NewEncoder(&network)    
	// 从二进制里面读取
	dec := gob.NewDecoder(&network)
	// Encode (send) the value.写数据      
	err := enc.Encode(P{3, 4, 5, "Pythagoras"})
	if err != nil {
		log.Fatal("encode error:", err)
	}
	// Decode (receive) the value.读取数据      
	var q Q
	err = dec.Decode(&q)
	if err != nil {
		log.Fatal("decode error:", err)
	}
	fmt.Printf("%q: {%d,%d}\n", q.Name, q.X, q.Y)
}
相关推荐
星星法术嗲人10 分钟前
【Java】—— 集合框架:Collections工具类的使用
java·开发语言
黑不溜秋的24 分钟前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光28 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
天上掉下来个程小白31 分钟前
Stream流的中间方法
java·开发语言·windows
xujinwei_gingko42 分钟前
JAVA基础面试题汇总(持续更新)
java·开发语言
sp_wxf1 小时前
Lambda表达式
开发语言·python
Fairy_sevenseven1 小时前
【二十八】【QT开发应用】模拟WPS Tab
开发语言·qt·wps
蜡笔小新星1 小时前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
凯子坚持 c1 小时前
C语言复习概要(三)
c语言·开发语言
无限大.1 小时前
c语言200例 067
java·c语言·开发语言