首頁  >  文章  >  後端開發  >  如何用go struct查看json的標籤鍵?

如何用go struct查看json的標籤鍵?

PHPz
PHPz轉載
2024-02-13 10:12:061206瀏覽

如何用go struct查看json的标签键?

問題內容

我正在學習https://www.digitalocean.com/community/tutorials/how-to-use-json-in-go#using -a-struct-to-generate-json(go的舊版)。

我用 go 1.20.1 、windows 11 x64、goland 2022.3.2 。

package sample3

import (
    foo "encoding/json"
    "fmt"
    "time"
)

type myjson struct {
    intvalue        int       `json:"intvalue"`
    boolvalue       bool      `json:"boolvalue"`
    stringvalue     string    `json:"stringvalue"`
    datevalue       time.time `json:"datevalue"`
    objectvalue     *myobject `json:"objectvalue"`
    nullstringvalue *string   `json:"nullstringvalue"`
    nullintvalue    *int      `json:"nullintvalue"`
}

type myobject struct {
    arrayvalue []int `json:"arrayvalue"`
}

func main3() {
    otherint := 4321
    data := &myjson{
        intvalue:    1234,
        boolvalue:   true,
        stringvalue: "hello!",
        datevalue:   time.date(2022, 3, 2, 9, 10, 0, 0, time.utc),
        objectvalue: &myobject{
            arrayvalue: []int{1, 2, 3, 4},
        },
        nullstringvalue: nil,
        nullintvalue:    &otherint,
    }
    fmt.println(foo.marshal(data))
    fmt.println(data)

    type myint struct {
        intvalue int
    }

    data2 := &myint{intvalue: 1234}
    fmt.println(foo.marshal(data2))

}

fmt.println(foo.marshal(data))

返回

&{1234 true hello! 2022-03-02 09:10:00 +0000 UTC 0xc000008240 <nil> 0xc00001a170}

我想查看 {"intvalue": 1234, "boolvalue": true, ...} ,請指導我。

完整原始碼https://github.com/donhuvy/vy_learn_go_json2023/blob/main/sample3/main3.go#l36

為什麼我使用 fmt.println(string(json.marshal(data))) 會導致錯誤?

解決方法

我通常使用json編碼庫。看看下面的例子:

package main

import (
    "encoding/json"
    "time"
)

type myjson struct {
    intvalue        int       `json:"intvalue"`
    boolvalue       bool      `json:"boolvalue"`
    stringvalue     string    `json:"stringvalue"`
    datevalue       time.time `json:"datevalue"`
    objectvalue     *myobject `json:"objectvalue"`
    nullstringvalue *string   `json:"nullstringvalue"`
    nullintvalue    *int      `json:"nullintvalue"`
}

type myobject struct {
    arrayvalue []int `json:"arrayvalue"`
}

func main() {
    otherint := 4321
    data := &myjson{
        intvalue:    1234,
        boolvalue:   true,
        stringvalue: "hello!",
        datevalue:   time.date(2022, 3, 2, 9, 10, 0, 0, time.utc),
        objectvalue: &myobject{
            arrayvalue: []int{1, 2, 3, 4},
        },
        nullstringvalue: nil,
        nullintvalue:    &otherint,
    }
    bytes, err := json.marshal(data)   // <-------------------this line
    println(string(bytes)) // <-------------------and this line
    println(err)
}

輸出:

{"intValue":1234,"boolValue":true,"stringValue":"hello!","dateValue":"2022-03-02T09:10:00Z","objectValue":{"arrayValue":[1,2,3,4]},"nullStringValue":null,"nullIntValue":4321}

以上是如何用go struct查看json的標籤鍵?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除