Json實例
簡介
人懶,直接從網上扒了篇文檔 《Go by Example: JSON》并稍加修改。
golang有內置json相關處理的包"encoding/json",支持內置類型和用戶自定類型。這篇文章主要介紹簡單的使用。
接口:
編碼:
func Marshal(v interface{}) ([]byte, error)
func NewEncoder(w io.Writer) *Encoder
[func (enc *Encoder) Encode(v interface{}) error
解碼:
func Unmarshal(data []byte, v interface{}) error
func NewDecoder(r io.Reader) *Decoder
func (dec *Decoder) Decode(v interface{}) error
正文
內置簡單類型沒有什么好說的,直接上coding:
bolB, _ := json.Marshal(true)
fmt.Println(string(bolB))
intB, _ := json.Marshal(1)
fmt.Println(string(intB))
fltB, _ := json.Marshal(2.34)
fmt.Println(string(fltB))
strB, _ := json.Marshal("gopher")
fmt.Println(string(strB))
slcD := []string{"apple", "peach", "pear"} slcB, _ := json.Marshal(slcD)
fmt.Println(string(slcB))
mapD := map[string]int{"apple": 5, "lettuce": 7} mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB))
結果:
true
1
2.34
"gopher"
["apple","peach","pear"]
{"apple":5,"lettuce":7}
自定義類型:
默認情況下,轉義所有公開的成員(大寫字母開頭的),私有成員(小寫字母開頭的)不轉義:
type Name struct {
First, Last string
}
n := Name{First: "Ta", Last: "SY"}
if r, e := json.Marshal(n); e == nil {
fmt.Println("name is ", string(r))
} else {
fmt.Println("err ", e)
}
結果:
name is {"First":"Ta","Last":"SY"}
可以通過tag來自定義json關鍵字;也可以直接轉義到其他流中去例如標準輸入輸出、文件、網絡:
這個是帶tag自定義json關鍵字,注意”user“是小寫的
type TagName struct {
Name string `json:"nm"`
Book string `json:"b"`
user string
}
enc := json.NewEncoder(os.Stdout)
tags := []TagName{
{"hello", "Joson", "weter"},
{"world", "Nano", "jober"},
}
if e := enc.Encode(tags); e != nil {
fmt.Println("e ", e)
}
輸出結果,注意上面私有成員user是沒有被轉義的:
[{"nm":"hello","b":"Joson"},{"nm":"world","b":"Nano"}]
下面這個是網絡的簡單完整示例:
package main
import (
"encoding/json"
"net/http"
)
var s store
type bigTask struct {
ID int
Status string
Data map[string]string
}
type store struct {
task []bigTask
}
var bigTasks []bigTask
func init() {
bigTasks = []bigTask{
{0, "wait", map[string]string{"data": "http://localhost/0/data"}},
{1, "done", map[string]string{"data": "http://localhost/1/data"}},
{2, "dead", map[string]string{"data": "http://localhost/2/data"}},
}
}
func main() {
http.HandleFunc("/big_task", bigtask)
http.ListenAndServe(":10004", nil)
}
func bigtask(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
if e := enc.Encode(bigTasks); e != nil {
http.Error(w, e.Error(), http.StatusInternalServerError)
}
}
運行后,訪問:"http://localhost:10004/big_task",輸出為:
[{"ID":0,"Status":"wait","Data":{"data":"http://localhost/0/data"}},{"ID":1,"Status":"done","Data":{"data":"http://localhost/1/data"}},{"ID":2,"Status":"dead","Data":{"data":"http://localhost/2/data"}}]
有幾個需要注意的地方
- json類型僅支持string作為關鍵字,因而轉義map時,map[int]T類型會報錯(T為任意類型)
- Channel, complex, and function types不能被轉義
- 不支持循環類型的數據,因為這會導致Marshal死循環
- 指針會被轉義為其所指向的值
解碼
還是比較懶,又扒了篇: 《JSON and Go》by Andrew Gerrand,鏈接為golang官網中的博客,要翻墻哦
用Unmarshal函數來解碼:
func Unmarshal(data []byte, v interface{}) error
首先要有個變量來存放解碼結果
var m Message
調用json.Unmarshal,參數為[]byte類型的json數據和指向m的指針
err := json.Unmarshal(b, &m)
如果成功,err為nil,同時m會被填充,類似如下賦值
m = Message{
Name: "Alice",
Body: "Hello",
Time: 1294706395881547000,
}
Unmarshal是如何進行的呢?假定json關鍵字為"Foo",流程如下:
- 先找tag為"Foo"的公開成員
- 再找名字為"Foo"的公開成員
- 最后再查找"FOO","FoO"等大小寫不敏感的公開成員
如果json數據沒有匹配到Go類型會出現什么結果?
type Message struct {
Name string
Body string
Time int64
}
b := []byte(`{"Name":"Bob","Food":"Pickle"}`)
var m Message
err := json.Unmarshal(b, &m)
Unmarshal僅僅解析匹配到的結果。上例中,只有Name會被解析,而"Food"則被忽略掉。當你想從非常大的json數據中解析特定數據時,這個技巧會非常有用