Note: This answer was written before the question was edited. In the original question &arr
was passed to json.Unmarshal()
:
unmarshaled := json.Unmarshal([]byte(dataJson), &arr)
You pass the address of arr
to json.Unmarshal()
to unmarshal a JSON array, but arr
is not an array (or slice), it is a struct value.
Arrays can be unmarshaled into Go arrays or slices. So pass arr.Array
:
dataJson := `["1","2","3"]`
arr := JsonType{}
err := json.Unmarshal([]byte(dataJson), &arr.Array)
log.Printf("Unmarshaled: %v, error: %v", arr.Array, err)
Output (try it on the Go Playground):
2009/11/10 23:00:00 Unmarshaled: [1 2 3], error: <nil>
Of course you don't even need the JsonType
wrapper, just use a simple []string
slice:
dataJson := `["1","2","3"]`
var s []string
err := json.Unmarshal([]byte(dataJson), &s)