Go语言JSON数据相互转换

时间:2024-01-24 08:39:08

结构体转json

结构体转json示例:

package main

import (
    "encoding/json"
    "fmt"
)

//用户
type User struct {
    UserName string `json:"username"`
    NickName string `json:"nickname"`
    Age      int    `json:"age"`
    Birthday string `json:"birthday"`
    Sex      string `json:"sex"`
    Email    string `json:"email"`
    Phone    string `json:"phone"`
}

//结构体转JSON
func structToJSON() {

    user := User{
        UserName: "itbsl",
        NickName: "jack",
        Age:      18,
        Birthday: "2001-08-15",
        Sex:      "itbsl@gmail.com",
        Phone:    "176XXXX6688",
    }

    data, err := json.Marshal(user)

    if err != nil {
        fmt.Println("json.marshal failed, err:", err)
        return
    }

    fmt.Printf("%s\n", string(data))
}

func main() {

    structToJSON()

}

输出结果:

{"username":"itbsl","nickname":"jack","age":18,"birthday":"2001-08-15","sex":"itbsl@gmail.com","email":"","phone":"176XXXX6688"}

map转json

map转json示例:

package main

import (
    "encoding/json"
    "fmt"
)

//map转json
func mapToJSON(tempMap *map[string]interface{}) string {

    data, err := json.Marshal(tempMap)

    if err != nil {
        panic(err)
    }

    return string(data)
}

func main() {

    tempMap := make(map[string]interface{})

    tempMap["username"] = "itbsl"
    tempMap["age"] = 18
    tempMap["sex"] = "male"

    str := mapToJSON(&tempMap)

    fmt.Println(str)

}

输出结果:

{"age":18,"sex":"male","username":"itbsl"}

int转json

int转json

package main

import (
    "encoding/json"
    "fmt"
)

func testInt() {
    var age = 100
    data, err := json.Marshal(age)
    if err != nil {
        fmt.Printf("json.marshal failed, err:", err)
        return
    }

    fmt.Printf("%s\n", string(data))
}

func main() {
    testInt()
}

输出结果:

100

slice转json

slice转json

package main

import (
    "encoding/json"
    "fmt"
)

//slice转json
func sliceToJSON(s []map[string]interface{}) string {

    data, err := json.Marshal(s)

    if err != nil {
        panic(err)
    }

    return string(data)

}

func main() {

    var s []map[string]interface{}

    tempMap := make(map[string]interface{})

    tempMap["username"] = "itbsl"
    tempMap["age"] = 18
    tempMap["sex"] = "male"

    s = append(s, tempMap)

    tempMap = make(map[string]interface{})

    tempMap["username"] = "jane"
    tempMap["age"] = 23
    tempMap["sex"] = "female"

    s = append(s, tempMap)

    str := sliceToJSON(s)

    fmt.Println(str)
}

输出结果:

[{"age":18,"sex":"male","username":"itbsl"},{"age":23,"sex":"female","username":"jane"}]

json反序列化为结构体

json转结构体:

package main

import (
    "encoding/json"
    "fmt"
)

//用户
type User struct {
    UserName string `json:"username"`
    NickName string `json:"nickname"`
    Age      int    `json:"age"`
    Birthday string `json:"birthday"`
    Sex      string `json:"sex"`
    Email    string `json:"email"`
    Phone    string `json:"phone"`
}

//结构体转JSON
func structToJSON() string {

    user := User{
        UserName: "itbsl",
        NickName: "jack",
        Age:      18,
        Birthday: "2001-08-15",
        Sex:      "itbsl@gmail.com",
        Phone:    "176XXXX6688",
    }

    data, err := json.Marshal(user)

    if err != nil {
        fmt.Println("json.marshal failed, err:", err)
        return ""
    }

    return string(data)
}

//json转结构体
func JSONToStruct(str string) {

    var user User

    err := json.Unmarshal([]byte(str), &user)

    if err != nil {

        panic(err)
    }

    fmt.Println(user)

}

func main() {

    JSONToStruct(structToJSON())
}

输出结果:

{itbsl jack 18 2001-08-15 itbsl@gmail.com  176XXXX6688}

json反序列化为map

json转map

package main

import (
    "encoding/json"
    "fmt"
)

//map转json
func mapToJSON(tempMap *map[string]interface{}) string {

    data, err := json.Marshal(tempMap)

    if err != nil {
        panic(err)
    }

    return string(data)
}

func JSONToMap(str string) {

    var tempMap map[string]interface{}

    err := json.Unmarshal([]byte(str), &tempMap)

    if err != nil {
        panic(err)
    }

    fmt.Println(tempMap)

}

func main() {

    tempMap := make(map[string]interface{})

    tempMap["username"] = "itbsl"
    tempMap["age"] = 18
    tempMap["sex"] = "male"

    str := mapToJSON(&tempMap)

    JSONToMap(str)
}

输出结果:

map[age:18 sex:male username:itbsl]