1

I like to unmarshal a JSON string using Decode():

var message Message
decoder := json.NewDecoder(s)
err = decoder.Decode(&message)

My data structure is

type Message map[string]interface{}

The test data is as follows:

{
  "names": [
    "HINDERNIS",
    "TROCKNET",
    "UMGEBENDEN"
  ], 
  "id":1189,
  "command":"checkNames"
}

It's working fine for numbers and strings, but with the string array I get following panic:

panic: interface conversion: interface is []interface {}, not []string
Michael
  • 6,823
  • 11
  • 54
  • 84

1 Answers1

2

this is not possible by conversion because a slice of struct != slice of interface it implements!
either you can get the elements one by one and put them into a []string like this: http://play.golang.org/p/1yqScF9yVX

or better, use the capabilities of the json package to unpack the data in your model format : http://golang.org/pkg/encoding/json/#example_Unmarshal

Community
  • 1
  • 1
Adam Vincze
  • 861
  • 6
  • 8
  • I chose the second. I changed the map[string]interface{} type into a struct {Id int Command string Names []string } and then used Decode() as I did before. Works fine. – Michael Aug 09 '15 at 17:54