First of all, you're ignoring the error return value from json.Unmarshal
. You probably want something like:
if err := json.Unmarshal([]byte(str), &s); err != nil {
log.Fatalln(err)
}
With that change, we can see that your JSON data isn't valid: invalid character 's' after object key:value pair
. There is a missing quote at the end of "dxh1
on the second line.
Fixing that error and rerunning the program you'll get a different error: json: cannot unmarshal object into Go value of type []main.Serverslice
. There are two possible problems here:
You meant to decode into an object. In this case, just declare
s
as aServerslice
. Here is a version of your program that makes that change: http://play.golang.org/p/zgyr_vnn-_Your JSON is supposed to be an array (possible, since it seems to have duplicate keys). Here's an updated version with the JSON changed to provide an array: http://play.golang.org/p/Wl6kUaivEm