反序列化为结构体
func DeSerializeStruct(){
jsonString := ‘{“ip”:”198.0.0.1”,”name”:”Demo”,”port”:9090}’
server := new(server)
err := json.Unmarsh([]byte(jsonString),&server)
if err != err{
fat.Println(err.Error())
}
fat.Println(server)
}
反序列化为map
func DeSerializeMap(){
jsonString := ‘{“ip”:”198.0.0.1”,”name”:”Demo”,”port”:9090}’
server := make(map[string]interface{})
err := json.Unmarsh([]byte(jsonString),&server)
if err != err{
fat.Println(err.Error())
}
fat.Println(server)
}
序列化与反序列化
type Sever struct{
ServerName string `json:”name”` json序列号是name替换 ServerName
ServerIp string `json:”ip”`
ServerPort int `json:”port”`
}
func SerializeStuct(){
server := new(Server)
server.SeverName = “server”
server.ServerIp= “127.0.0.1”
server.ServerPort = 8080
b,eer := json.Marshal(server) //序列化成json 字节数组
if err != nil{
fat.Println(err.Error())
return
}
fat.Println(string(b)) // json字符串
}
func SerializeMap(){
server := make(map[string]interface{})
server[“SeverName”]= “server”
server[“ServerIp”]= “127.0.0.1”
server[“ServerPort”] = 8080
b,eer := json.Marshal(server) //序列化成json 字节数组
if err != nil{
fat.Println(err.Error())
return
}
fat.Println(string(b)) // json字符串
}