Skip to main content

JSON

[TOC]

综合的例子

package main

import (
"encoding/json"
"fmt"
)
// 如果`json:"code"`去掉.会以字段名称为解析内容
type Result struct {
Code int `json:"code"`
Message string `json:"msg"`
}

func main() {
var res Result
res.Code = 200
res.Message = "success"

//序列化
jsons, errs := json.Marshal(res)
if errs != nil {
fmt.Println("json marshal error:", errs)
}
fmt.Println("json data :", string(jsons))

//反序列化
var res2 Result
errs = json.Unmarshal(jsons, &res2)
if errs != nil {
fmt.Println("json unmarshal error:", errs)
}
fmt.Println("res2 code:", res2.Code)
fmt.Println("res2 msg:", res2.Message)


// json.Valid 判断是json结构是否正确
jsonStr1 := `{"code":200,"message":"dddd"}`
fmt.Print("jsonStr1是否合法:", json.Valid([]byte(jsonStr1)), "\n")
var res2 Result
fmt.Print(json.Unmarshal([]byte(jsonStr1), &res2), res2)
}

序列化

例子1 序列化结构体

import (
"encoding/json"
"fmt"
)

type Server struct {
ServerName string
ServerIp string
ServerPort int
}

func SerializeStruct(){
server := new(Server)

server.ServerName = "Demo-for-json-struct"
server.ServerIp = "127.0.0.1"
server.ServerPort = 8080

b,err := json.Marshal(server) //序列化成json字节数组
if err != nil {
fmt.Println( "Marshal err :",err.Error())
return
}
fmt.Println("Marshal json :", string(b)) // 将json字节数组转成string
}

结果

Marshal json : {"ServerName":"Demo-for-json-struct","ServerIp":"127.0.0.1","ServerPort":8080}

例子2 序列化结构体 (加了Tag)

//定义了Tag
type Server struct {
ServerName string `json:"name"`
ServerIp string `json:"ip"`
ServerPort int `json:"port"`
}

func SerializeStruct(){
server := new(Server)

server.ServerName = "Demo-for-json-struct"
server.ServerIp = "127.0.0.1"
server.ServerPort = 8080

b,err := json.Marshal(server) //序列化成json字节数组
if err != nil {
fmt.Println( "Marshal err :",err.Error())
return
}
fmt.Println("Marshal json :", string(b)) // 将json字节数组转成string
}

执行结果

Marshal json : {"name":"Demo-for-json-struct","ip":"127.0.0.1","port":8080}

例子3 序列化结构体Map

func SerializeMap(){
server := make(map[string]interface{})

server["ServerName"] = "Demo-for-json-map"
server["ServerIp"] = "127.0.0.1"
server["ServerPort"] = 8080
b,err := json.Marshal(server) //序列化成json字节数组
if err != nil {
fmt.Println( "Marshal err :",err.Error())
return
}
fmt.Println("Marshal json :", string(b)) // 将json字节数组转成string
}

结果

Marshal json : {"ServerIp":"127.0.0.1","ServerName":"Demo-for-json-map","ServerPort":8080}

反序列化

例子1 反序列化结构体

type Server struct {
ServerName string
ServerIp string
ServerPort int
}
func DeSerializeStruct() {
jsonString := `{"ServerName":"Demo-for-json-struct","ServerIp":"127.0.0.1","ServerPort":8080}`
server := new(Server)
err := json.Unmarshal([]byte(jsonString),&server)
if err != nil {
fmt.Println( "Unmarshal err : ",err.Error() )
}
fmt.Println("Unmarshal struct : ",server)
}

结果

Unmarshal struct :  &{Demo-for-json-struct 127.0.0.1 8080}

例子2 反序列化map

type Server struct {
ServerName string
ServerIp string
ServerPort int
}
func DeSerializeMap() {
jsonString := `{"ServerName":"Demo-for-json-map","ServerIp":"127.0.0.1","ServerPort":8080}`
server := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonString),&server)
if err != nil {
fmt.Println( "Unmarshal err : ",err.Error() )
}
fmt.Println("Unmarshal struct : ",server)
}

结果

Unmarshal struct :  map[ServerIp:127.0.0.1 ServerName:Demo-for-json-map ServerPort:8080]