mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:06:41 +08:00
完成12.9章节
This commit is contained in:
231
eBook/12.9.md
Normal file
231
eBook/12.9.md
Normal file
@@ -0,0 +1,231 @@
|
||||
# Json数据格式
|
||||
|
||||
数据结构要在网络中传输或保存到文件,就必须对其编码和解码;目前存在很多编码格式:JSON,XML,gob,Google缓冲协议等等。Go语言支持所有这些编码格式;在后面的章节,我们将讨论前三种格式。
|
||||
|
||||
结构能够包含二进制数据,如果作为文本打印,那么可读性是很差的。另外结构内部包含命名字段,所以不清楚数据的用意。
|
||||
|
||||
通过把数据转换成纯文本,使用命名的字段来标注,让其具有可读性。这样的数据格式可以通过网络传输,而且是与平台无关的,任何类型的应用都能够读取和输出,不用关系操作系统和编程语言的类型。
|
||||
|
||||
下面是一些术语说明:
|
||||
|
||||
- 数据结构 --> 指定格式 = `序列化`或`编码`(传输之前)
|
||||
- 指定格式 --> 数据格式 = `反序列化`或`解码`(传输之后)
|
||||
|
||||
序列化是在内存中把数据转换成指定格式(data -> string),反之亦然(string -> data structure)
|
||||
|
||||
编码也是一样的,只是输出一个数据流(实现了io.Writer接口);解码是从一个数据流(实现了io.Reader)输出到一个数据结构。
|
||||
|
||||
我们都比较熟悉XML格式(参阅12.10);但有些时候JSON(JavaScript Object Notation,参阅 [http://json.org](http://json.org))被作为首选,主要是由于其格式上非常简洁。通常json被用在web后端和浏览器之间通讯,但是在其它场景也同样的有用。
|
||||
|
||||
这是一个简短的JSON片段:
|
||||
|
||||
```javascript
|
||||
{
|
||||
"Person": {
|
||||
"FirstName": "Laura",
|
||||
"LastName": "Lynn"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
尽管XML被广泛的应用,但是JSON更加简洁、轻量(其占用更少的内存、磁盘及网络带宽)和更好的可读性,这也说明他越来越受欢迎。
|
||||
|
||||
go语言的json包可以让你在程序中方便的读取和写入JSON数据。
|
||||
|
||||
我们将在下面的例子里使用json包,并使用练习10.1 vcard.go中一个简化版本的Address和VCard结构(为了简单起见,我们忽略了很多错误处理,不过在实际应用中你必须要合理的处理这些错误,参阅13章)
|
||||
|
||||
|
||||
示例 12.16 [json.go](examples/chapter_12/json.go):
|
||||
|
||||
```go
|
||||
// json.go.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
Type string
|
||||
City string
|
||||
Country string
|
||||
}
|
||||
|
||||
type VCard struct {
|
||||
FirstName string
|
||||
LastName string
|
||||
Addresses []*Address
|
||||
Remark string
|
||||
}
|
||||
|
||||
func main() {
|
||||
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:
|
||||
js, _ := json.Marshal(vc)
|
||||
fmt.Printf("JSON format: %s", js)
|
||||
// using an encoder:
|
||||
file, _ := os.OpenFile("vcard.json", os.O_CREATE|os.O_WRONLY, 0)
|
||||
defer file.Close()
|
||||
enc := json.NewEncoder(file)
|
||||
err := enc.Encode(vc)
|
||||
if err != nil {
|
||||
log.Println("Error in encoding json")
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
`json.Marshal()`的函数签名是 `func Marshal(v interface{}) ([]byte, error)`,下面是数据编码后的json文本(实际上是一个[]bytes):
|
||||
|
||||
```javascript
|
||||
{
|
||||
"FirstName": "Jan",
|
||||
"LastName": "Kersschot",
|
||||
"Addresses": [{
|
||||
"Type": "private",
|
||||
"City": "Aartselaar",
|
||||
"Country": "Belgium"
|
||||
}, {
|
||||
"Type": "work",
|
||||
"City": "Boom",
|
||||
"Country": "Belgium"
|
||||
}],
|
||||
"Remark": "none"
|
||||
}
|
||||
```
|
||||
|
||||
处于安全考虑,在web应用中最好使用`json.MarshalforHTML()`函数,其对数据执行HTML转码,所以文本会被安全的嵌在HTML`<script>`标签中。
|
||||
|
||||
JSON与go类型对应如下:
|
||||
|
||||
- bool对应JSON的booleans
|
||||
- float64对应JSON的numbers
|
||||
- string对应JSON的strings
|
||||
- nil对应JSON的null
|
||||
|
||||
不是所有的数据都可以编码为json类型:只有验证通过的数据结构才能被编码:
|
||||
|
||||
- JSON对象只支持字符串类型的key;要编码一个go map类型,map必须是map[string]T(T是json包中支持的任何类型)
|
||||
- Channel,复杂类型和函数类型不能被编码
|
||||
- 不支持循环数据结构;它将引起序列化进入一个无线循环
|
||||
- 指针可以被编码,实际上是对指针指向的值进行编码(或者指针是nil)
|
||||
|
||||
### 反序列化:
|
||||
|
||||
`UnMarshal()`的函数签名是 `func Unmarshal(data []byte, v interface{}) error` 把json解码为数据结构。
|
||||
|
||||
我们首先创建一个结构Message用来保存解码的数据:`var m Message` 并调用`Unmarshal()`,解析[]byte中的json数据并将结果存入指针m指向的值
|
||||
|
||||
虽然反射能够让json字段去尝试匹配目标结构字段;但是只有真正匹配上的字段才会填充数据。字段没有匹配不会报错,而是直接忽略掉。
|
||||
|
||||
(练习15.2b twitter_status_json.go中用到了UnMarshal)
|
||||
|
||||
### 解码任意的数据:
|
||||
|
||||
json包使用map[string]interface{}和[]interface{}储存任意的JSON对象和数组;其可以被反序列化为任何的JSON blob存储到接口值中。
|
||||
|
||||
来看这个JSON数据,被存储在变量b中:
|
||||
|
||||
```go
|
||||
b == []byte({"Name": "Wednesday", "Age": 6, "Parents": ["Gomez", "Morticia"]})
|
||||
```
|
||||
|
||||
不用理解这个数据的结构,我们可以直接使用Unmarshal把这个数据编码并保存在接口值中:
|
||||
|
||||
```go
|
||||
var f interface{}
|
||||
err := json.Unmarshal(b, &f)
|
||||
```
|
||||
|
||||
f指向的值是一个map,key是一个字符串,value是自身存储作为空接口类型的值:
|
||||
|
||||
```go
|
||||
map[string]interface{}{
|
||||
"Name": "Wednesday",
|
||||
"Age": 6,
|
||||
"Parents": []interface{}{
|
||||
"Gomez",
|
||||
"Morticia",
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
要访问这个数据,我们可以使用类型断言
|
||||
|
||||
```go
|
||||
m := f.(map[string]interface{})
|
||||
```
|
||||
|
||||
我们可以通过for range语法和type switch来访问其实际类型:
|
||||
|
||||
```go
|
||||
for k, v := range m {
|
||||
switch vv := v.(type) {
|
||||
case string:
|
||||
fmt.Println(k, "is string", vv)
|
||||
case int:
|
||||
fmt.Println(k, "is int", 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")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
通过这种方式,你可以处理未知的JSON数据,同时可以确保类型安全。
|
||||
|
||||
### 解码数据到结构:
|
||||
|
||||
如果我们事先知道json数据,我们可以定义一个适当的结构并对json数据反序列化。下面的例子中,我们将定义:
|
||||
|
||||
```go
|
||||
type FamilyMember struct {
|
||||
Name string
|
||||
Age int
|
||||
Parents []string
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
并对其反序列化:
|
||||
|
||||
```go
|
||||
var m FamilyMember
|
||||
err := json.Unmarshal(b, &m)
|
||||
```
|
||||
|
||||
程序实际上是分配了一个新的切片。这是一个典型的反序列化引用类型(指针、切片和映射)的例子。
|
||||
|
||||
### 编码和解码流
|
||||
json包提供Decoder和Encoder类型来支持常用的读写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.Decoder和Decode()函数:
|
||||
|
||||
```go
|
||||
func NewDecoder(r io.Reader) *Decoder
|
||||
func (dec *Decoder) Decode(v interface{}) error
|
||||
```
|
||||
|
||||
来看下接口是如何对实现进行抽象的:数据结构可以是任何类型,只要其实现了某种接口,目标或来源数据要能够被编码就必须实现io.Writer或io.Reader接口。由于go语言中到处都实现了Reader和Writer,因此Encoder和Decoder可被应用的场景非常广泛,例如读取或写入HTTP连接、websockets或文件。
|
||||
|
||||
## 链接
|
||||
|
||||
- [目录](directory.md)
|
||||
- 上一节:[一个使用接口的实际例子:fmt.Fprintf](12.8.md)
|
||||
- 下一节:[XML数据格式](12.10.md)
|
Reference in New Issue
Block a user