【问题标题】:json.Marshal(struct) returns "{}"json.Marshal(struct) 返回“{}”
【发布时间】:2014-12-07 06:35:02
【问题描述】:
type TestObject struct {
    kind string `json:"kind"`
    id   string `json:"id, omitempty"`
    name  string `json:"name"`
    email string `json:"email"`
}

func TestCreateSingleItemResponse(t *testing.T) {
    testObject := new(TestObject)
    testObject.kind = "TestObject"
    testObject.id = "f73h5jf8"
    testObject.name = "Yuri Gagarin"
    testObject.email = "Yuri.Gagarin@Vostok.com"

    fmt.Println(testObject)

    b, err := json.Marshal(testObject)

    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(string(b[:]))
}

这是输出:

[ `go test -test.run="^TestCreateSingleItemResponse$"` | done: 2.195666095s ]
    {TestObject f73h5jf8 Yuri Gagarin Yuri.Gagarin@Vostok.com}
    {}
    PASS

为什么 JSON 本质上是空的?

【问题讨论】:

    标签: json go marshalling


    【解决方案1】:

    您需要通过将字段名称中的第一个字母大写来export TestObject 中的字段。将kind 更改为Kind 等等。

    type TestObject struct {
     Kind string `json:"kind"`
     Id   string `json:"id,omitempty"`
     Name  string `json:"name"`
     Email string `json:"email"`
    }
    

    encoding/json 包和类似的包会忽略未导出的字段。

    字段声明后面的`json:"..."` 字符串是struct tags。此结构中的标签在与 JSON 进行编组时设置结构字段的名称。

    Ru it on the playground.

    【讨论】:

    • “omitempty”之前应该没有“空格”
    • 我可以用小写字母吗?
    • 如果你想用小写字母标记字段stackoverflow.com/questions/21825322/…
    • @user123456 使用 json 字段标记将 JSON 字段名称设置为小写名称(如本答案最后一段所述)。
    【解决方案2】:
    • 当第一个字母大写时,标识符对任何人都是公开的 您要使用的一段代码。
    • 当首字母为小写时,标识符为私有且 只能在它声明的包内访问。

    例子

     var aName // private
    
     var BigBro // public (exported)
    
     var 123abc // illegal
    
     func (p *Person) SetEmail(email string) {  // public because SetEmail() function starts with upper case
        p.email = email
     }
    
     func (p Person) email() string { // private because email() function starts with lower case
        return p.email
     }
    

    【讨论】:

    • 很棒的人,完美的工作只是将第一个字母改为大写,非常感谢
    • 没错,In Go, a name is exported if it begins with a capital letter。把它放在上下文中,访问这个Go Basics Tour
    【解决方案3】:

    在 golang 中

    struct 中首字母必须大写 前任。电话号码 -> 电话号码

    ======= 添加详细信息

    首先,我正在尝试这样的编码

    type Questions struct {
        id           string
        questionDesc string
        questionID   string
        ans          string
        choices      struct {
            choice1 string
            choice2 string
            choice3 string
            choice4 string
        }
    }
    

    golang 编译没有错误,也没有显示警告。但是响应是空的,因为某事

    之后,我搜索谷歌找到了这篇文章

    结构类型和结构类型文字 Article 然后...我尝试编辑代码。

    //Questions map field name like database
    type Questions struct {
        ID           string
        QuestionDesc string
        QuestionID   string
        Ans          string
        Choices      struct {
            Choice1 string
            Choice2 string
            Choice3 string
            Choice4 string
        }
    }
    

    工作。

    希望得到帮助。

    【讨论】:

    • 添加更多细节
    • Yapp,我添加更多细节。
    猜你喜欢
    • 2021-01-02
    • 2022-12-02
    • 2021-11-26
    • 1970-01-01
    • 2017-10-11
    • 2021-07-07
    • 2013-10-12
    • 1970-01-01
    相关资源
    最近更新 更多