golang的解组结构

I need to create a struct to unmarshal and access key "name" and receive its value.

{
    "product":"car",
    "spec":[
        {
            "name":"bla_bla",
            "info":{
                "color":"black",
                "year":1991
            }
        }
    ]
} 

Here is what my struct looks like now:

type Products struct {
Product string `json:"product"`
Specs []Spec  `json:"spec"`
}

type Spec struct {
    Name string  `json:"name"`
    Info Inf `json:"info"`
}

type Inf struct {
    Color string `json:"color"`
    Year  int  `json:"year"`
}

With this struct I can only access Products.Product, the Products.Specs shows only [ ].

I checked json.unmarshal errors and origin response from websocket I found out that my json string look little bit diffrent.

 c :=  `{"product":"car","spec":["{\"name\":\"bla_bla\",\"info\":{\"color\":\"black\",\"year\":\"1991\"}}"]}`

I used strings to replaced some chars and decoder. Now it works how I wanted it to work.

s_replacer := strings.NewReplacer(`"{`, "{", `"]`, "]", "\\", "")
z := s_replacer.Replace(c)
dec := json.NewDecoder(strings.NewReader(z))

for {
    var m Products
    if err := dec.Decode(&m); err == io.EOF {
        break
    } else if err != nil {
        log.Fatal(err)
    }
    fmt.Println(m.Specs[0].Name)
}

I think the problem was quotes in square bracket it treat it as array of string. Anyways thank for response I am still new to golang.

It seems that you have already found a workaround for your problem, but you don't need to work around it.

I'm not sure what language you're using to provide this JSON data, but you're turning the Specs into strings within the array.

For example, the return from JSON.stringify({ "name": "test" }) in Javascript will be {"name":"test"}. However, JSON.stringify('{ "name": "test" }') will return
"{ \\"name\\": \\"test\\" }".