Hello everyone.
Here is my struct
type Form struct {
Key string `json:"key"`
Label string `json:"label"`
Type string `json:"type"`
NotNull bool `json:"notNull"`
Enums []Enum `json:"enums"`
Custom *Form `json:"custom"`
}
here is the JSON
{
"key": "schoolInfo",
"placeHolder": "",
"label": "school Info",
"type": "Custom",
"notNull": false,
"enums": [],
"custom": [
{
"key": "departmentId",
"placeHolder": "",
"label": "Department",
"type": "Id",
"notNull": false,
"enums": [],
"custom": [],
"tags": []
},
{
"key": "studentId",
"placeHolder": "",
"label": "Student Id",
"type": "String",
"notNull": false,
"enums": [],
"custom": [],
"tags": []
},
{
"key": "currentDegreeType",
"placeHolder": "",
"label": "DegreeType",
"type": "Enum",
"notNull": false,
"enums": [
{
"label": "BACHELOR",
"value": "BACHELOR"
},
{
"label": "MASTER",
"value": "MASTER"
},
{
"label": "DOCTOR",
"value": "DOCTOR"
},
{
"label": "SUCCESSIVE_POSTGRADUATE_AND_DOCTORAL",
"value": "SUCCESSIVE_POSTGRADUATE_AND_DOCTORAL"
},
{
"label": "FOREIGN_STUDENT",
"value": "FOREIGN_STUDENT"
},
{
"label": "COLLEGE_DEGREE",
"value": "COLLEGE_DEGREE"
}
],
"custom": [],
"tags": []
},
{
"key": "majorId",
"placeHolder": "",
"label": "Major",
"type": "Id",
"notNull": false,
"enums": [],
"custom": [],
"tags": []
},
{
"key": "grade",
"placeHolder": "",
"label": "Grade",
"type": "String",
"notNull": false,
"enums": [],
"custom": [],
"tags": []
},
{
"key": "estimatedGraduationTime",
"placeHolder": "",
"label": "",
"type": "Date",
"notNull": false,
"enums": [],
"custom": [],
"tags": []
},
{
"key": "departmentGrade",
"placeHolder": "",
"label": "",
"type": "String",
"notNull": false,
"enums": [],
"custom": [],
"tags": []
}
],
"tags": []
}
Here is the code I was unmarshaling the JSON to the struct.
var form form_definition.Form
err := json.Unmarshal([]byte(aJson), &form)
if err != nil {
logrus.Error("Found err!")
println(err)
}else {
println(form.Key)
}
But I got an error. The error looks so strange for me
Here is the error
time="2019-08-23T17:39:29+08:00" level=error msg="Found err!"
(0x531a80,0xc0000960a0)
When I modify the struct to below everything is fine.
type Form struct {
Key string `json:"key"`
Label string `json:"label"`
Type string `json:"type"`
NotNull bool `json:"notNull"`
Enums []Enum `json:"enums"`
//Custom *Form `json:"custom"`
}
My question is how can I convert the JSON to the struct?
Thank you all.