GO教程
GO控制语句
GO高级

Go JSON

Go JSON

Go 内置了对 JSON 编码和解码的支持。它还支持自定义数据类型。
Marshal 函数用于将 go 数据类型转换为 JSON 格式。
Marshal 函数语法为:
"func Marshal(v interface{}) ([]byte, error)"
Marshal 返回 v 的 JSON 编码。
Boolean 转换为 JSON 布尔值。浮点数、整数和数字被转换为 JSON 数字。映射的键类型必须是字符串、整数类型或实现 encoding.TextMarshaler。
JSON 的解码是使用 Unmarshal 函数完成的。
解组函数语法为:
"func Unmarshal(data []byte, v interface{}) error"
Unmarshal 解码 JSON 编码的值并将结果存储在 v 指向的值中。如果 v 为 nil 或不是指针,则 Unmarshal 返回 InvalidUnmarshalError。
我们还可以自定义存储的字段在结构字段标签中的"json"键下。我们可以有字段名称,后跟逗号分隔的选项列表。喜欢
Field int 'json:"myName"' // The appears in JSON as key "myName".
Field int 'json:"myName,omitempty?'// The field is omitted from the object if its value is empty,
Field int 'json:"-"' //// Field is ignored by this package.

转到 JSON 示例 1

package main
import "encoding/json"
import "fmt"
func main() {
    bolType, _ := json.Marshal(false) //boolean Value
    fmt.Println(string(bolType))
    intType, _ := json.Marshal(10) // integer value
    fmt.Println(string(intType))
    fltType, _ := json.Marshal(3.14) //float value
    fmt.Println(string(fltType))
    strType, _ := json.Marshal("lidihuo") // string value
    fmt.Println(string(strType))
    slcA := []string{"sun", "moon", "star"} //slice value
    slcB, _ := json.Marshal(slcA)
    fmt.Println(string(slcB))
    mapA := map[string]int{"sun": 1, "moon": 2} //map value
    mapB, _ := json.Marshal(mapA)
    fmt.Println(string(mapB))
}
输出:
     false
10
3.14
"lidihuo"
["sun","moon","star"]
{"moon":2,"sun":1}

Go JSON 示例 2(用户定义的数据类型)

package main
import (
    "encoding/json"
    "fmt"
    "os"
)
type Response1 struct {
    Position   int
    Planet []string
}
type Response2 struct {
    Position   int      'json:"position"'
    Planet []string 'json:"planet"'
}
func main()  {
    res1A := &Response1{
        Position:   1,
        Planet: []string{"mercury", "venus", "earth"}}
    res1B, _ := json.Marshal(res1A)
    fmt.Println(string(res1B))
    res2D := &Response2{
        Position:   1,
        Planet: []string{"mercury", "venus", "earth"}}
    res2B, _ := json.Marshal(res2D)
    fmt.Println(string(res2B))
    byt := []byte('{"pi":6.13,"place":["New York","New Delhi"]}`)
    var dat map[string]interface{}
    if err := json.Unmarshal(byt, &dat); err != nil {
        panic(err)
    }
    fmt.Println(dat)
    num := dat["pi"].(float64)
    fmt.Println(num)
    strs := dat["place"].([]interface{})
    str1 := strs[0].(string)
    fmt.Println(str1)
    
    
    str := `{"Position": 1, "Planet": ["mercury", "venus"]}`
    res := Response2{}
    json.Unmarshal([]byte(str), &res)
    fmt.Println(res)
    fmt.Println(res.Planet[1])
    enc := json.NewEncoder(os.Stdout)
    d := map[string]string{"1":"mercury" , "2": "venus"}
    enc.Encode(d)
}
输出:
{"Position":1,"Planet":["mercury","venus","earth"]}
{"position":1,"planet":["mercury","venus","earth"]}
map[pi:6.13 place:[New York new Delhi]]
6.13
New York
{1 [mercury venus]}
venus
{"1":"mercury","2":"venus"}

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4