Home > Back-end >  GoLang Unable to decode JSON object with dynamic type
GoLang Unable to decode JSON object with dynamic type

Time:06-23

I am working on structure that looks something like this:

"requestFieldValues": [
    {
        "fieldId": "string",
        "label": "string",
        "value": "string"
    },
    {
        "fieldId": "string",
        "label": "string",
        "value": "string,
        "renderedValue": {
            "html": "string"
        }
    },
    {
        "fieldId": "priority",
        "label": "Priority",
        "value": {
            "self": "string",
            "iconUrl": "string",
            "name": "string",
            "id": "string"
        }
    },
    {
        "fieldId": "attachment",
        "label": "Attachment",
        "value": []
    },
],

Following this structure I am unable to fix the value of value property. So far, I have tried solving it with

type RequestFieldValues struct {
    FieldId string                 `json:"fieldId"`
    Label   string                 `json:"label"`
    Value   map[string]interface{} `json:"value"`
}

and

type RequestFieldValues struct {
   FieldId string                 `json:"fieldId"`
   Label   string                 `json:"label"`
   Value   string                 `json:"value"`
}

but I am unable to find any work around with Value property.

CodePudding user response:

Using the following struct worked for me.

type RequestFieldValues struct {
    FieldId string      `json:"fieldId"`
    Label   string      `json:"label"`
    Value   interface{} `json:"value"`
}

CodePudding user response:

type PcList struct {
    Total int64       `json:"total"`
    Data  interface{} `json:"data"` // PCResource
}

type PcResource struct {
    Id primitive.ObjectID `bson:"_id"`
    DiskPercent float64 `json:"diskPercent"`
    CpuPercent float64 `json:"cpuPercent"`
    CreateTime int64   `json:"crateTime"`
}
  • Related