代码之家  ›  专栏  ›  技术社区  ›  Vy Do

如何使用go结构查看json的标记键?

  •  0
  • Vy Do  · 技术社区  · 3 年前

    我在学习 https://www.digitalocean.com/community/tutorials/how-to-use-json-in-go#using-a-struct-to-generate-json (旧版围棋)。

    我使用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}
    

    enter image description here

    enter image description here

    我想看 {"IntValue": 1234, "BoolValue": true, ...} ,请引导我。

    完整源代码 https://github.com/donhuvy/vy_learn_go_json2023/blob/main/sample3/main3.go#L36

    enter image description here

    我为什么使用 fmt.Println(string(json.Marshal(data))) 导致错误?

    2 回复  |  直到 3 年前
        1
  •  2
  •   TaQuangTu    3 年前

    我通常使用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}
    
        2
  •  2
  •   recursion    3 年前

    您没有正确格式化输出

    执行以下操作,有关fmt的详细信息,请查看此处 go_fmt

        d, _ := foo.Marshal(data)
        fmt.Printf("%+v \n", string(d))
        fmt.Printf("%+v \n", *data)
    
        type myInt struct {
            IntValue int
        }
    
        data2 := &myInt{IntValue: 1234}
        d2, _ := foo.Marshal(data2)
        fmt.Printf("%+v", string(d2))