1 Introduction
For persistent storage of data in memory or the network switch is often employed json format string, go languages json
package json serialization and deserialization
to Go provides basic types and can be used directly in line with type package json serialization and de serialization operation
for the structure can be obtained by converting the relationship between tags and attributes declared json string, called the tag json, common format
is:
⚫ json:
default form, may be omitted, using the keys json attribute names, attribute types by the corresponding type
⚫ json: "key"
json key names with the specified name key
⚫ json: "-"
this property is ignored
⚫ json: "key, of the type"
json key name with the specified name key, type the specified type of the type
⚫ json: "key, omitempty "
JSON key name specified name key, the value of this attribute is omitted when a zero value
⚫ JSON:" key, type, omitempty "
JSON key name specified name key, the type of the specified type type, a zero value when the value thereof is omitted this property
⚫ json: ", type"
type specified type type
⚫ JSON: ", omitempty"
omitting the attribute value to a zero value
2. Common Functions
Marshal: sequence data for language go into json string
Indent: The format string json
UnMarshal: a deserialized data string json go language
MarshalIndent: go language for data serialization as json format string
Valid: verify that the correct json string
3.Encoder 与 Decoder
Encoder and Decoder are constructed JSON serialization and deserialization on stream constructor respectively
NewEncoder newDecoder and, respectively, to achieve the parameters required interfaces and io.Reader io.Writer object
Encoder.Encode serialization
Decoder.Decode deserialization of
Example 4
(1) convert a string
package main
import (
"encoding/json"
"fmt"
)
func main() {
//json.Marshal() 序列化 内存=>字符串/字节切片
//json.Unmarshal() 反序列化 字符串/字节切片 =>内存
//转换json格式
selice := []string{"aa","aaaa","aaaaaa"}
bytes ,err := json.Marshal(selice)
fmt.Printf("%v %v \n",string(bytes),err)
//json转换 字符串切片
var name []string
err = json.Unmarshal(bytes,&name)
fmt.Println(name)
//字符串切片 转换json的时候格式化
bytes ,err = json.MarshalIndent(name,"","\t")
if err == nil {
fmt.Println(string(bytes))
}
//验证是否可以转换为json
fmt.Println(json.Valid([]byte("[aa aaaa aaaaaa]")))
}
(2) converting structure
package main
import (
"encoding/json"
"fmt"
)
type Addr struct {
No int `json:"no"`
Street string `json:"street"`
}
//结构体要进行序列化或者反序列化 内部元素属性必须是公开的也就是大写
type User struct {
ID int`json:"-"` //这个表示忽略
Name string
Age int`json:"age,int,omitempty"` //第一个表示属性名 第二个表示类型 第三个表示 如果是零值就忽略
desc string
Addr Addr `json:"addr"`
}
func main() {
user :=User{
ID: 1,
Name: "b",
Age: 0,
desc: "c",
Addr: Addr{
No: 1,
Street: "aa",
},
}
bytes ,_ := json.MarshalIndent(user,"","\t")
fmt.Println(string(bytes))
var users User
json.Unmarshal(bytes,&users)
fmt.Println(users)
}
(3) Custom Type Conversion
package main
import (
"encoding/json"
"fmt"
)
//自定义类型json转换
const (
Large = iota //large
Medium //medium
Small //small
)
type Size int
func (s Size) MarshalText() ([]byte,error){
switch s {
case Large:
return []byte("large"),nil
case Medium:
return []byte("medium"),nil
case Small:
return []byte("small"),nil
default:
return []byte("unknow"),nil
}
}
func (s *Size) UnmarshalText(byte []byte) error{
switch string(byte) {
case "large":
*s = Large
case "medium":
*s = Medium
case "small":
*s = Small
default:
*s = Small
}
return nil
}
func main() {
var size Size = Medium
bytes,_ := json.Marshal(size)
fmt.Println(string(bytes))
json.Unmarshal(bytes,&size)
fmt.Println(size)
sizes := []Size{Large,Large,Small,Medium}
bytes,_ = json.Marshal(sizes)
fmt.Println(string(bytes))
var size02 []Size
json.Unmarshal(bytes,&size02)
fmt.Println(size02)
}