[Solved] How to decode embedded json? [closed]


There are several issues in your code.

  1. First of all, all the fields of your edit struct must be exported which means you should capitalize the first letter of every fields.
    Just like the following:

    type edit struct {
        Id
        Values
    }
    
  2. The data type of of key of the Values field must be string to be able to decode the json data.

    type edit struct {
        Id     int
        Values map[string]interface{}
    }
    
  3. Finally, data type of your json data "id" is wrong. It must be int to be matched to your Go struct. Also, there is no "m" defined in your struct. The "m" must be replaced with "values" to match your Go struct. Just like the following

    {
        "id": 5,
        "values":{
            "name": "alice"
        }
    }
    

Just to let you know, you can simply use following piece of code to unmarshal json data into Go struct.

err := json.Unmarshal([]byte(a), &s)

1

solved How to decode embedded json? [closed]