首页 > 后端开发 > Golang > 正文

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

PHPz
发布: 2024-02-13 10:12:06
转载
1203 人浏览过

如何用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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!