How can i unmarshal json to the struct?


(noName) #1

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.


(noName) #2

Sorry. The type of the custom field of the JSON is an array. But in my struct, the custom is not an array. This is the reason why I am got the error


(Jaron R. Swab) #3

Welcome to the forums!

Sounds like you figure it out. Any other questions about json marshaling?


(noName) #4

Thank you. I don`t have other question about json marshaling. The way of marshalling json in go is so easy. I love golang. I just remembered there is a question about marshalling json. How can I know an json type ? I need to know the json is an array or is an object. Do you know how to get the type of a json ? Than you again.