json.Marshal(struct) returns “{}”

后端 未结 3 1554
Happy的楠姐
Happy的楠姐 2020-11-22 07:17
type TestObject struct {
    kind string `json:\"kind\"`
    id   string `json:\"id, omitempty\"`
    name  string `json:\"name\"`
    email string `json:\"email\"`
         


        
3条回答
  •  太阳男子
    2020-11-22 07:55

    In golang

    in struct first letter must uppercase ex. phonenumber -> PhoneNumber

    ======= Add detail

    First, I'm try coding like this

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

    golang compile is not error and not show warning. But response is empty because something

    After that, I search google found this article

    Struct Types and Struct Type Literals Article then... I try edit code.

    //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
        }
    }
    

    Is work.

    Hope for help.

提交回复
热议问题